我正在尝试使用Jacoco与JUnit 5和Spring Boot生成代码覆盖率报告。我试图使用JUnit 5的DynamicTest功能。它成功运行但是jacoco生成的测试覆盖率报告中没有涵盖动态测试。目前Jacoco还没有涵盖JUnit 5动态测试吗?
以下是代码:
@RunWith(JUnitPlatform.class)
@SelectPackages("com.troll.jpt.abc.model")
@SelectClasses({Status.class})
public class DynamicModelTester {
private Status status;
@BeforeEach
public void setUp() {
status = new Status();
}
@TestFactory
public Stream<DynamicTest> checkDynamicTestsFromStream() {
List<String> input = Arrays.asList("abc");
List<String> output = Arrays.asList("abc");
status.setCode(input.get(0));
return input.stream().map(str -> DynamicTest.dynamicTest("status test", () -> {
assertEquals(output.get(0), status.getCode());
}));
}
}
我使用的jacoco插件如下:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2-SNAPSHOT</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
答案 0 :(得分:3)
它成功运行,但jacoco生成的测试覆盖率报告中未涵盖动态测试。
jacoco-maven-plugin:report
在测试中不显示覆盖范围,它显示测试目标的覆盖范围 - 在您的情况下,目标似乎是类Status
,其定义完全不存在。对于将来,我强烈建议您阅读https://stackoverflow.com/help/mcve并遵循其建议,尤其是关于“完成”的部分:
确保包含重现问题所需的所有信息
关于“JaCoCo是否支持JUnit 5动态测试?”等问题的通用答案。是: JaCoCo通过JUnit 4或JUnit 5,甚至手动或其他任何执行方式记录事实,即代码的执行方式独立执行。
关于“为什么有些代码没有标记为涵盖?”之类的问题的通用答案。是:确保代码实际执行,如果是自动测试,这意味着 - 确保实际执行这些测试。
但是让我们试试JUnit 5 Dynamic Test。如果没有src/main/java/Status.java
,我会认为它类似于
public class Status {
private String code;
public void setCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
不知道您为什么需要systemPropertyVariables
,jacoco-maven-plugin
的快照版本,report
的多次执行以及dataFile
的冗余规范及其默认值,因此也会假设在稍微清理之后完成pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
放置后
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
@RunWith(JUnitPlatform.class)
@SelectPackages("com.troll.jpt.abc.model")
@SelectClasses({Status.class})
public class DynamicModelTester {
private Status status;
@BeforeEach
public void setUp() {
status = new Status();
}
@TestFactory
public Stream<DynamicTest> dynamicTestsFromStream() {
List<String> input = Arrays.asList("abc");
List<String> output = Arrays.asList("abc");
status.setCode(input.get(0));
return input.stream().map(str -> DynamicTest.dynamicTest("status test", () -> {
assertEquals(output.get(0), status.getCode());
}));
}
}
进入src/test/java/DynamicModelTester.java
并执行mvn clean verify
:
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ example ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ example ---
[INFO] Building jar: /private/tmp/jacoco/target/example-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.1:report (default) @ example ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
最后一行非常可疑,maven-surefire-plugin
执行的测试数量不足以及target/surefire-reports/
缺席。
让我们将DynamicModelTester
重命名为DynamicModelTest
并再次执行mvn clean verify
:
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ example ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running DynamicModelTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 s - in DynamicModelTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ example ---
[INFO] Building jar: /private/tmp/jacoco/target/example-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.1:report (default) @ example ---
[INFO] Loading execution data file /private/tmp/jacoco/target/jacoco.exec
[INFO] Analyzed bundle 'example' with 1 classes
与之前的尝试相比,这次执行了测试。覆盖率报告target/jacoco-ut/default/Status.html
如下所示:
我认为未执行测试的原因在于default value of includes
of maven-surefire-plugin
:
指定测试的元素列表(按模式) 应该包括在测试中。未指定时和测试时 未指定参数,默认包含
<includes> <include>**/Test*.java</include> <include>**/*Test.java</include> <include>**/*Tests.java</include> <include>**/*TestCase.java</include> </includes>
DynamicModelTester.java
与这些模式中的任何一个都不匹配,而DynamicModelTest.java
匹配第二,但我会将此验证作为一个小练习。