美文网首页Appium
Android代码覆盖率初探—问题已解决!

Android代码覆盖率初探—问题已解决!

作者: Lacia | 来源:发表于2017-09-08 11:48 被阅读0次

    6月份到时候就要搞Android代码覆盖率,但是当时客户端即将改版,所以就耽搁了。在Android大哥的帮助下,终于完成了初步的代码覆盖率统计。

    在github上找到了封装好的jacocoTestHelper:https://github.com/Jay-Goo/JacocoTestHelper,在splash页调用JacocoHelper.init(),在退出app时调用JacocoHelper.generateEcFile(true)

    gradle配置:

    apply plugin: 'com.android.application'
    apply plugin: 'jacoco'
    def coverageSourceDirs = [
            '../app/src/main/java'
    ]
    
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
    
        defaultConfig {
            applicationId "com.test.app"
            minSdkVersion 18
            targetSdkVersion 25
            versionCode 37
            versionName "1.8.20.5"
    
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
        }
        buildTypes {
            debug {
                testCoverageEnabled = true
            }
            release {
                testCoverageEnabled = true
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        lintOptions{
            checkReleaseBuilds false
            abortOnError false
        }
    }
    
    task jacocoTestReport(type: JacocoReport) {
        group = "Reporting"
        description = "Generate Jacoco coverage reports after running tests."
        reports {
            xml.enabled = true
            html.enabled = true
        }
        classDirectories = fileTree(
                dir: './build/intermediates/classes/debug',
                excludes: ['**/R*.class',
                           '**/*$InjectAdapter.class',
                           '**/*$ModuleAdapter.class',
                           '**/*$ViewInjector*.class'
                ])
        sourceDirectories = files(coverageSourceDirs)
        executionData = files("$buildDir/outputs/code-coverage/connected/coverage.ec")
    
        doFirst {
            new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
                if (file.name.contains('$$')) {
                    file.renameTo(file.path.replace('$$', '$'))
                }
            }
        }
    }
    
    dependencies {
        compile 'com.github.Jay-Goo:JacocoTestHelper:v0.0.2'
    }
    

    现在仍存在问题:
    手动执行时,执行结束按home退出,再重新打开app时,覆盖率统计可以叠加。如果用appium执行测试,每次open application时会重启app(我猜),重新调用JacocoHelper.init(),这样每执行一条都会生成一个coverage.ec文件,如何用多个ec文件生成覆盖率报告是未来要解决的问题,或者说,是否存在一种方法让appium open application的时候不进行杀掉重启。啊,任重道远啊。

    另:最近发现用python直接写case并没有比用RF麻烦,而且执行速度感觉比RF快一点。RF的优点在于可以直观管理用例,android方面的关键字自己还是需要封装一些,现有的AppiumLibrary不能满足需求。

    另2:此时特想搞个mbp,但是就以现在买买买的节奏,啥时候能攒下来钱哇(望天)


    20170912
    问题得以解决哈哈哈哈哈哈哈,稍晚更新解决方案,开心哈哈哈哈哈哈哈哈哈

    20171101
    更新:
    appium每执行完一个case,点击两次回退退出app,将/mnt/sdcard/coverage.ec导出并重命名到指定路径(本文为$buildDir/outputs/code-coverage/connected/)

    在gradle中增加:

    task removeOldMergeEc(type: Delete) {
        delete "$buildDir/outputs/code-coverage/connected/mergedcoverage.ec"
    }
    
    
    task mergeReport(type:JacocoMerge,dependsOn:removeOldMergeEc){
        group = "Reporting"
        description = "merge jacoco report."
        destinationFile= file("$buildDir/outputs/code-coverage/connected/mergedcoverage.ec")
        //"$buildDir/outputs/code-coverage/connected/"为存放.ec文件的路径
        FileTree tree = fileTree("$buildDir/outputs/code-coverage/connected/") {
            include '**/*.ec'
        }
        executionData = tree
    }
    

    并将上文task jacocoTestReport(type: JacocoReport)中以下代码进行修改:

    executionData = files("$buildDir/outputs/code-coverage/connected/coverage.ec)
    改为:
    executionData = files("$buildDir/outputs/code-coverage/connected/mergedcoverage.ec")
    

    所有case执行结束后,执行

    gradlew mergeReport -Pec_dir="F:\Mox\Mox\mox-portal\build\outputs\code-coverage\connected"
    

    确认"$buildDir/outputs/code-coverage/connected/路径下已生成mergedcoverage.ec后

    gradlew jacocoTestReport
    

    然后就可以开心的查看报告啦~

    PS:以上参考了一位大佬的方法,但是链接找不到了。在此鸣谢。


    Reference:
    doctorQ:https://testerhome.com/topics/2524
    思寒:https://testerhome.com/topics/522

    相关文章

      网友评论

        本文标题:Android代码覆盖率初探—问题已解决!

        本文链接:https://www.haomeiwen.com/subject/leegjxtx.html