Jacoco 代码覆盖率 Android Studio 不考虑 androidTest 文件夹
Jacoco code coverage Android Studio not consider androidTest folder
我正在尝试在我的 Android 项目上激活 Jacoco,用于单元测试(test
文件夹)和仪器测试(androidTest
文件夹)。真的我对instrumented测试最感兴趣,因为我的测试都放在这里。
我可以成功执行这两种类型的测试,但是当我尝试创建覆盖率报告(使用 gradlew createDebugCoverageReport
命令)时,没有考虑仪器测试。
此外,当我右键单击文件夹时,只有 test
文件夹中的测试显示 "run with Coverage" 选项,如果我转到 "Edit configuration" 只有 JUnit 下的配置有 "Code Coverage" 选项卡,在 "Android Tests" 配置中没有看到它。
这是我的 build.gradle:
apply plugin: 'com.android.application'
//apply plugin: "jacoco"
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "com.abc.myapp"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
// Enabling multidex support.
multiDexEnabled true
// testInstrumentationRunner 'com.android.test.runner.MultiDexTestRunner'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled false
}
debug {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
testOptions {
unitTests.returnDefaultValues = true
}
productFlavors {
}
sourceSets {
main {
resources.srcDirs = ['src/main/java']
}
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
// androidTest.setRoot('src/androidTest')
}
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 'NOTICE'
exclude 'LICENSE.txt'
exclude 'LICENSE'
exclude 'asm-license.txt'
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.0.1'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:24.1.1'
androidTestCompile 'com.android.support:support-annotations:24.0.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// // Optional -- UI testing with Espresso
// androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
// // Optional -- UI testing with UI Automator
// androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
}
此 Link 将逐步指导您获得 Jacoco Coverage.
gradlew createDebugAndroidTestCoverageReport 为应用模块中的插桩测试创建代码覆盖率报告,您可以在
中找到它
/build/reports/coverage/debug > index.html
我正在尝试在我的 Android 项目上激活 Jacoco,用于单元测试(test
文件夹)和仪器测试(androidTest
文件夹)。真的我对instrumented测试最感兴趣,因为我的测试都放在这里。
我可以成功执行这两种类型的测试,但是当我尝试创建覆盖率报告(使用 gradlew createDebugCoverageReport
命令)时,没有考虑仪器测试。
此外,当我右键单击文件夹时,只有 test
文件夹中的测试显示 "run with Coverage" 选项,如果我转到 "Edit configuration" 只有 JUnit 下的配置有 "Code Coverage" 选项卡,在 "Android Tests" 配置中没有看到它。
这是我的 build.gradle:
apply plugin: 'com.android.application'
//apply plugin: "jacoco"
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "com.abc.myapp"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
// Enabling multidex support.
multiDexEnabled true
// testInstrumentationRunner 'com.android.test.runner.MultiDexTestRunner'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled false
}
debug {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
testOptions {
unitTests.returnDefaultValues = true
}
productFlavors {
}
sourceSets {
main {
resources.srcDirs = ['src/main/java']
}
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
// androidTest.setRoot('src/androidTest')
}
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 'NOTICE'
exclude 'LICENSE.txt'
exclude 'LICENSE'
exclude 'asm-license.txt'
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.0.1'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:24.1.1'
androidTestCompile 'com.android.support:support-annotations:24.0.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// // Optional -- UI testing with Espresso
// androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
// // Optional -- UI testing with UI Automator
// androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
}
此 Link 将逐步指导您获得 Jacoco Coverage.
gradlew createDebugAndroidTestCoverageReport 为应用模块中的插桩测试创建代码覆盖率报告,您可以在
中找到它/build/reports/coverage/debug > index.html