Jacoco:对于报告生成,必须使用与运行时相同的类文件
我一直在做一个Android项目,并使用roboletric和powermock来做单元测试。
当我运行时,它会显示gradle jacocoTestReport
[ant:jacocoReport] Classes in bundle 'app' do no match with execution data. For report generation the same class files must be used as at runtime.
[ant:jacocoReport] Execution data for class com/my/app/MyClass does not match.
我使用powermock来模拟Myclass中的静态方法.java
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest(MyClass.class)
public class TheTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
public void test1() throws Exception {
PowerMockito.mockStatic(MyClass.class);
// do something
}
}
build.gradle 如下所示
apply plugin: 'jacoco'
def coverageSourceDirs = [
'../app/src/main/java'
]
task jacocoTestReport(type:JacocoReport, dependsOn: "testDebugUnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../app/build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../app/build/jacoco/testDebugUnitTest.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
我仍然可以看到报道报告没有失真。
但是如何摆脱这样的警告呢?