pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Jacoco 和單元測試代碼覆蓋率與 android-gradle-plugi

Jacoco and Unit Tests Code Coverage with android-gradle-plugin gt;= 1.1(Jacoco 和單元測試代碼覆蓋率與 android-gradle-plugin = 1.1)
本文介紹了Jacoco 和單元測試代碼覆蓋率與 android-gradle-plugin >= 1.1的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我最近開始在我的一個項目中集成 android-gradle-plugin 1.1.0.該項目使用 robolectric 2.4 來運行單元測試.

I recently started integrating android-gradle-plugin 1.1.0 in one of my projects. The project uses robolectric 2.4 to run unit tests.

這是一個多模塊項目,具有非常復雜的依賴關系(一些模塊依賴于其他模塊).類似的東西:

It's a multi module project with very complex dependencies (Some modules depend on other modules). Something like that:

--> application-module (dependsOn: module1, module2, module-core)
    --> module1 (dependsOn: module-core)
    --> module2 (dependsOn: module-core)
    --> module-core (dependsOn: module3, module4)
        --> module3 (library dependencies)
        --> module4 (library dependencies)

如需更清晰的圖片,請參閱 jacoco-example 項目.

For a more cleared picture please see jacoco-example project.

我試圖集成 JaCoCo 來為單元測試生成報告,但在我看來它只運行 androidTests 基本上是儀器測試.

I tried to integrate JaCoCo to generate reports for the unit tests, but it seems to me that it runs only androidTests which are basically instrumentation tests.

經過一些谷歌搜索后,我在 GitHub 和其他文章上遇到了一些項目,但它們主要集中在以前版本的 android-gradle-plugin 或正在使用其他第三方android-unit-test 例如這里這樣的插件.

After some google'ing I've come across a few projects on GitHub and other articles, but they mainly are focused on previous versions of the android-gradle-plugin or are using other third party plugins like android-unit-test for example here.

可能是我失去了使用谷歌搜索的能力.但是有人可以指出我可以找到一些關于 android gradle 插件中的新內容以及如何僅針對單元測試運行 jacoco 任務的文檔的方向嗎?

May be I've lost my ability to google. But can somebody point me in a direction where I can find some documentations regarding the new stuff in android gradle plugin and how to run the jacoco task only for unit tests?

更新

采用 nenick 的例子的腳本:

apply plugin: "jacoco"

configurations {
    jacocoReport
}

task jacocoReport(dependsOn: 'testDebug') << {
    ant {
        taskdef(name:'jacocoreport',
                classname: 'org.jacoco.ant.ReportTask',
                classpath: configurations.jacocoReport.asPath)

        mkdir dir: "${buildDir}/test-coverage-report"
        mkdir dir: "${buildDir}/reports/jacoco/test/"

        jacocoreport {
            executiondata = files("${buildDir}/jacoco/testDebug.exec")

            structure(name: "${rootProject.name}") {
                classfiles {
                    fileset (dir: "${buildDir}/intermediates/classes/debug") {
                        //exclude(name: '**/*_*.class')
                        exclude(name: '**/R.class')
                        exclude(name: '**/R$*.class')
                        exclude(name: '**/BuildConfig.class')
                    }
                }

                sourcefiles {
                    fileset dir: "src/main/java"
                    fileset dir: "${buildDir}/generated/source/buildConfig/debug"
                    fileset dir: "${buildDir}/generated/source/r/debug"
                }
            }

            xml destfile: "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
            html destdir: "${buildDir}/test-coverage-report/"
        }
    }
}

dependencies {
    jacocoReport 'org.jacoco:org.jacoco.ant:0.7.2.201409121644'
}

之后,./gradlew jacocoReport 執行并生成報告,但它顯示 0(零)測試覆蓋率,這是不可能的,因為至少有一半的類都經過測試.

After that the ./gradlew jacocoReport executes and generates the report, but it shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.

UPDATE_2

試用了這個示例.將下一個任務添加到我的一個 gradle 構建文件中:

Tried out this example. Adding the next task to one of my gradle build files:

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: "${buildDir}/intermediates/classes/debug",
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )

    sourceDirectories = files("${buildDir.parent}/src/main/java")
    additionalSourceDirs = files([
            "${buildDir}/generated/source/buildConfig/debug",
            "${buildDir}/generated/source/r/debug"
    ])
    executionData = files("${buildDir}/jacoco/testDebug.exec")

    reports {
        xml.enabled = true
        html.enabled = true
    }
}

同樣的問題,生成了報告,但代碼覆蓋率仍然為零.

Same issue, the reports are generated, but the code coverage is still zero.

UPDATE_3

UPDATE_2 中的任務似乎有效,但僅適用于帶有 apply plugin: 'com.android.application' 的模塊(報告正確生成).但是對于 android 庫模塊(apply plugin: 'com.android.library'),報告顯示零覆蓋率,盡管模塊包含的測試比應用程序模塊多.

It seams that the task from UPDATE_2 worked but only for the module with apply plugin: 'com.android.application' (The reports a generated correctly). But for modules that are android libraries (apply plugin: 'com.android.library') the reports show zero coverage, although the modules contain more tests then the application module.

UPDATE_4

創建了一個簡單的示例項目來演示我的問題.目前,如果您運行 ./gradlew jacocoReport 會生成報告,但不會顯示模塊項目的測試覆蓋率.請參閱此 鏈接

Created a simple example project that demonstrates my issue. Currently if you run ./gradlew jacocoReport the report is generated, but no test coverage is displayed for the module projects. See this link

簡短說明:當測試是 AndroidUnitTests(白化 JUnit 4 和 Robolectric)時,JaCoCo 報告顯示了所有模塊的覆蓋范圍.

Short note: When the tests were AndroidUnitTests (whiteout JUnit 4 and Robolectric) JaCoCo reports showed coverage for all the modules.

有什么想法嗎?

推薦答案

經過一番折騰,我決定創建一個 開源 Gradle 插件 .

After the hassle, I decided to create an open source Gradle plugin for that.

根 build.gradle

buildscript {
    repositories {
        mavenCentral() // optional if you have this one already
    }
    dependencies {
        classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.16.0'
    }
}

apply plugin: 'com.vanniktech.android.junit.jacoco'

然后直接執行

./gradlew jacocoTestReportDebug

它將在調試模式下運行 JUnit 測試,然后在相應的構建目錄中以 XML 和 HTML 形式為您提供 Jacoco 輸出.

It'll run the JUnit tests in Debug Mode and then give you the Jacoco output in XML and HTML form in the corresponding build directory.

它還支持風味.將創建紅色和藍色 2 種口味的任務

It also supports flavors. Having 2 flavors red and blue those tasks would be created

  • jacocoTestReportRedDebug
  • jacocoTestReportBlueDebug
  • jacocoTestReportRedRelease
  • jacocoTestReportBlueRelease

這篇關于Jacoco 和單元測試代碼覆蓋率與 android-gradle-plugin >= 1.1的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現 IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當前風味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風味庫的多風味應用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | 印刷人才网 印刷、包装、造纸,中国80%的印刷企业人才招聘选印刷人才网! | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 课件导航网_ppt课件_课件模板_课件下载_最新课件资源分享发布平台 | BHK汞灯-百科|上海熙浩实业有限公司| 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | 包塑软管|金属软管|包塑金属软管-闵彬管业 | 游戏版号转让_游戏资质出售_游戏公司转让-【八九买卖网】 | 牛奶检测仪-乳成分分析仪-北京海谊| 不锈钢酒柜|恒温酒柜|酒柜定制|酒窖定制-上海啸瑞实业有限公司 | 北京公司注册_代理记账_代办商标注册工商执照-企力宝 | 福建省教师资格证-福建教师资格证考试网 | 山东PE给水管厂家,山东双壁波纹管,山东钢带增强波纹管,山东PE穿线管,山东PE农田灌溉管,山东MPP电力保护套管-山东德诺塑业有限公司 | 气动绞车,山东气动绞车,气动绞车厂家-烟台博海石油机械有限公司 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 | 照相馆预约系统,微信公众号摄影门店系统,影楼管理软件-盟百网络 | 分类168信息网 - 分类信息网 免费发布与查询 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 细砂提取机,隔膜板框泥浆污泥压滤机,螺旋洗砂机设备,轮式洗砂机械,机制砂,圆锥颚式反击式破碎机,振动筛,滚筒筛,喂料机- 上海重睿环保设备有限公司 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 金属管浮子流量计_金属转子流量计厂家-淮安润中仪表科技有限公司 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 凝胶成像系统(wb成像系统)百科-上海嘉鹏 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 汽车水泵_汽车水泵厂家-瑞安市骏迪汽车配件有限公司 | 电动垃圾车,垃圾清运车-江苏速利达机车有限公司| 天津市能谱科技有限公司-专业的红外光谱仪_红外测油仪_紫外测油仪_红外制样附件_傅里叶红外光谱技术生产服务厂商 | 长沙网站建设制作「网站优化推广」-网页设计公司-速马科技官网 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 广东之窗网| 磁力链接搜索神器_BT磁力狗_CILIMAO磁力猫_高效磁力搜索引擎2024 | 智能监控-安防监控-监控系统安装-弱电工程公司_成都万全电子 |