gpt4 book ai didi

android - 使用 gradle 从 android 测试到 Sonar 的代码覆盖率

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:26 25 4
gpt4 key购买 nike

我必须在我的 android 测试中设置代码覆盖率报告,然后将它们发布到 Sonar 中。我读过没有工具和插件可以做到这一点。我正在使用 gradle 脚本,我尝试了 jacoco 插件、cobertura,但没有结果。有什么方法可以解决吗?我也试着在这里做 Gradle jacoco code coverage - Then publish/show in Jenkins

最佳答案

具有代码覆盖率和 Sonar 的 build.gradle 示例

apply plugin: 'com.android.application'

android {

compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
applicationId 'com.example.coverage'
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName '1.0'
testHandleProfiling true
testFunctionalTest true

}
buildTypes {
debug {
testCoverageEnabled true
}
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {

apply plugin: 'sonar-runner'
}

packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/services/javax.annotation.processing.Processor'
exclude 'LICENSE.txt'
}
}

sonarRunner {

sonarProperties {

property "sonar.projectKey", "coverage-example"
property "sonar.projectName", "Coverage Example"
property "sonar.projectVersion", "1.0"

property "sonar.sources", "src/main/java"
property "sonar.binaries", "build"
property "sonar.test", "src/androidTest/java"

property "sonar.profile", "Sonar way"
property "sonar.language", "java"
property "sonar.sourceEncoding", "UTF-8"

property "sonar.dynamicAnalysis", "reuseReports"

property "sonar.junit.reportsPath", "build/outputs/reports/coverage/debug"
property "sonar.cobertura.reportPath", "build/outputs/reports/coverage/debug/cobertura.xml"
property "sonar.java.coveragePlugin", "cobertura"

property "sonar.host.url", "http://localhost:9099"
}
}

现在运行

gradlew clean assembleDebug createDebugCoverageReport

现在您将能够在该目录中看到包含代码覆盖率报告的 xml

app/build/outputs/reports/coverage/debug/report.xml

现在,在我们运行 Sonar 任务之前,我们必须将 report.xml 转换为 Sonar 可以读取的内容,它将使用在 python 脚本中完成的操作,该脚本将此文件转换为插件 sonar cobertura 可以使用的格式

Link the script on github

python cover2cover.py app/build/outputs/reports/coverage/debug/report.xml src/main/java > app/build/outputs/reports/coverage/debug/cobertura.xml && cp app/build/outputs/androidTest-results/connected/* app/build/outputs/reports/coverage/debug/

现在运行

gradlew sonarRunner

关于android - 使用 gradle 从 android 测试到 Sonar 的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22631551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com