我将我的工作从单个Hudson机器转移到多从属Jenkins环境,现在JaCoCo覆盖不再有效.
工作(旧):Hudson 2.0.1,Jenkins Sonar插件1.7.1,声纳2.1.2
BROKEN(新):Jenkins 1.446,Jenkins Sonar Plugin 1.7.2,Sonar 2.1.2
我的Hudson工作被称为Pinnacle,它曾经在同一台机器上使用Sonar在单个Hudson服务器上运行.我通过执行以下操作来设置我的(NO MAVEN)构建.
1)在我的build.xml中添加了一个Ant目标 test-with-coverage
2)在Hudson中配置Pinnacle作业以"使用以下属性调用独立声纳分析":
sonar.projectKey=com.skyboximaging:pinnacle
sonar.projectName="Pinnacle"
sonar.projectVersion=1.0
sources=Pinnacle/src/java
tests=Pinnacle/test/java
binaries=Pinnacle/classes
sonar.jacoco.reportPath=Pinnacle/jacoco.exec
sonar.jacoco.antTargets=test-with-coverage
Run Code Online (Sandbox Code Playgroud)
(请注意,代码将签出到Jenkins作业工作区的Pinnacle目录中.)
3)配置声纳"常规设置"以使用JaCoCo进行代码覆盖
一切都很美妙!
但是在新的Jenkins环境中,我在Jenkins构建输出中看到了这个错误:
23:15:17.863 INFO Sensor JaCoCoSensor...
23:15:17.868 INFO Project coverage is set to 0% as no JaCoCo execution data has been dumped: /var/lib/jenkins/workspace/Pinnacle/Pinnacle/jacoco.exec
Run Code Online (Sandbox Code Playgroud)
该文件在构建运行的从站上不存在.(目录/ var/lib/jenkins/workspace/Pinnacle/Pinnacle 确实存在.)
所有其他传感器(FindBugs,PMD等)似乎正常工作.只是JaCoCo坏了.
Sonar/JaCoCo甚至可以在多从属Jenkins环境中工作吗?
我怀疑Ant任务test-with-coverage没有运行.Sonar如何找到build.xml?新老装置有什么不同?
正如标题所说,这就是问题所在.我有一个带有模块的android风格的应用程序.该应用程序在android-studio和TeamCity服务器中使用gradle编译.我们有自定义任务来创建看起来像这样的代码覆盖:
task codeCoverageReport(type: JacocoReport) {
// Gather execution data from all subprojects
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
//Build class & source dirs
def classExcludes =['**/R.class',
'**/R$*.class',
'**/BuildConfig.class',
'**/*$InjectAdapter*.class',
'**/*$ModuleAdapter*.class',
'**/*$ViewInjector*.class']
sourceDirectories = files();
classDirectories = files();
subprojects.each{
sourceDirectories += files(it.projectDir.absolutePath + '/src/main/java')
def path1 = it.buildDir.absolutePath + '/intermediates/classes/debug'
def path2 = it.buildDir.absolutePath + '/classes/main'
classDirectories += fileTree(dir: path1, excludes: classExcludes, includes: ['**/*.class'])
classDirectories += fileTree(dir: path2, excludes: classExcludes, includes: ['**/*.class'])
}
reports {
xml.enabled true
html.enabled true
html.destination "${buildDir}/reports/jacoco"
csv.enabled false
}
doFirst …Run Code Online (Sandbox Code Playgroud) 我正在一个巨大的代码库上运行集成测试.在覆盖率报告中,我想知道哪个测试覆盖了代码中的某一行.无论如何使用jacoco吗?
信息:java 版本:1.8.0_66 蚂蚁版本:1.9.6
我想做的事:
为在 AWS windows 2k12 服务器上运行的服务器代码提供代码覆盖率报告。
我做了什么:
结果:
出现错误无法读取执行数据文件 C:/path/to/exec/jacoco.exec
构建.xml:
<project name="Example" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant">
<description>
Example Ant build file that demonstrates how a JaCoCo coverage report
can be itegrated into an existing build in three simple steps.
</description>
<property name="result.dir" location="." />
<property name="result.classes.dir" …Run Code Online (Sandbox Code Playgroud) 尝试使用Jacoco在Android上的Robolectric测试中获得代码覆盖率,但它在创建报告时拒绝承认我的Robolectric测试.
我的jacoco.gradle文件如下:
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.6.201602180812"
}
project.afterEvaluate {
android.applicationVariants.all { variant ->
def name = variant.name
def testTaskName = "test${name.capitalize()}UnitTest"
tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${name.capitalize()} build."
classDirectories = fileTree(
dir: "${project.buildDir}/intermediates/classes/${name}",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
sourceDirectories = files(['src/main/java'].plus(android.sourceSets[name].java.srcDirs))
executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
reports {
xml.enabled = true
html.enabled = true
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有了这个设置,我可以得到覆盖报告,但我得到0%的覆盖率,尽管在"的src/test/java下"有Robolectric测试.
如果我将以下代码添加到该文件:
android { …Run Code Online (Sandbox Code Playgroud) 我有一个build.gradle文件,可以单独或一起成功运行单元和集成测试(使用gradle test integrationTest等命令).两者都使用Junit,我使用的是Gradle 3,这不是Android项目.在不同的目录中为每个成功生成一份报告.我还可以使用gradle test jacoco成功生成Jacoco报告以进行单元测试覆盖.我无法通过使用gradle integrationTest jacoco获取我的其他工作集成测试的覆盖率报告,尽管测试成功运行并生成了integrationTest.exec文件.
为了更明确,我在build/reports/index.html中获得了单元测试覆盖率报告,在build/reports/test/index.html和build/reports/integrationTest/index.html中获得了Junit报告.创建了build/reports/jacoco目录,但只包含一个空的"test"目录.build /还包含一个jacoco /目录,其中包含.exec文件和classpathdumps.
这是我的缩写build.gradle
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'jacoco'
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = ['src/java']
}
test {
java {
srcDirs = ['test/java']
}
}
resources {
srcDirs = ['src/java', 'conf', 'resource']
}
}
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('integration_test/java')
}
}
} …Run Code Online (Sandbox Code Playgroud) 我们有一个带有默认方法的接口,我们在两个Java和Kotlin类中实现了该接口,我们提供了非默认方法的实现.
当我们在调试模式(没有testCoverageEnabled = true)运行时,应用程序按预期工作.但是当我们使用testCoverageEnabled = true在不同的配置中运行时,应用程序崩溃时出现以下错误
java.lang.NoSuchMethodError: No static method $$triggerInterfaceInit()V in class Lcom/ui/viewholders/CAViewContract$$CC; or its super classes (declaration of 'ui.viewholders.CAViewContract$$CC' appears in /data/app/SMCXbiLYvHb1Kk08Kee__g==/base.apk)
at home.c.CCFragment.<clinit>(Unknown Source:0)
at home.HomePageCardProvider.getFragment(HomePageCardProvider.java:17)
at home.HomeFragment.handleCardFragment(HomeFragment.java:172)
Run Code Online (Sandbox Code Playgroud)
注意: 1.JaCoCo版本:"0.8.0"2.操作系统:Android with minSdk 21
如果我们将minSdk更改为24,它testCoverageEnabled = true本身就可以正常工作.我们无法弄清楚确切的问题.
我的maven项目目录结构如下:
parent
-child
-src/java
-src/test <!-- unit tests -->
-pom.xml
-integration_test
-src/test
-pom.xml
-report
-pom.xml
-pom.xml
Run Code Online (Sandbox Code Playgroud)
由于集成测试和源代码位于不同的模块中,因此我使用了 Jacoco report-aggragate (报告模块)。
父pom.xml
<profile>
<id>codecoverage-it</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Run Code Online (Sandbox Code Playgroud)
报告模块pom.xml
<dependencies>
<dependency>
<groupId>###</groupId>
<artifactId>child</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>###</groupId>
<artifactId>integration_test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>overage-aggregate</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin> …Run Code Online (Sandbox Code Playgroud) 考虑以下最小的Kotlin示例:
fun <U> someWrapper(supplier: () -> U): () -> (U) {
return { supplier() }
}
fun foo(taskExecutor: TaskExecutor): Int {
val future = CompletableFuture.supplyAsync(someWrapper {
42
}, taskExecutor::execute)
return future.join()
}
@Test
public void shouldFoo() {
assertThat(foo(), is(42));
}
Run Code Online (Sandbox Code Playgroud)
我在Jacoco中有分支覆盖规则,该规则对上面的代码失败,说someWrapper呼叫行未覆盖2个分支中的1个。不幸的是,对于我来说,排除所有someWrapper被调用的类不是一个选择。
查看反编译的Java代码:
public final int foo(TaskExecutor taskExecutor) {
Object var10000 = WrappersKt.someWrapper((Function0)null.INSTANCE);
if (var10000 != null) {
Object var2 = var10000;
var10000 = new Foo$sam$java_util_function_Supplier$0((Function0)var2);
}
Supplier var3 = (Supplier)var10000;
Function1 var4 = (Function1)(new Function1(this.taskExecutor) { …Run Code Online (Sandbox Code Playgroud) Jacoco 无法覆盖仅包含静态方法的类。我没有在测试类中实例化该类,而是直接调用静态方法来测试。
public class DateUtil {
final static String datePattern = "EEE MM/dd/yyyy";
public static String convertToGMTDate(Date date ) {
DateFormat df = new SimpleDateFormat(datePattern, Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(date);
}
}
class DateUtilTest {
static DateUtil dateutil;
@Test
void convertToGMTDate() {
Date date = new GregorianCalendar(2020, Calendar.FEBRUARY, 11).getTime();
String stringDate = dateutil.convertToGMTDate(date);
assertEquals("Tue 02/11/2020)",stringDate);
}
}
Run Code Online (Sandbox Code Playgroud)
错误报告仅突出显示了类名“DateUtil”,并报告其已覆盖 75%。怎样做才能让课堂覆盖率达到100%?
不在测试方法中实例化该类,此处的覆盖率降低了 25%。这有什么意义呢?JaCoCo有缺陷吗?
提前致谢!