假设我有一个测试
@Test
public void testA(){
new A().doSomthing();
}
假设它包含一个方法 doSomething()
,现在在我的项目中,我有 1000 万个测试,这个测试只是其中之一。小测试作用不大。
现在让我们说我的doSomething
方法如下所示:-
public void doSomething() {
if (var1)
killMylSelf();
else if (var2)
killMyMother();
else
killMySelfAndMyMother();
}
正如您所看到的,该方法中有很多分支,因此会调用具有更多分支的其他方法。当我运行 testA
时,我想看看我在执行的代码中遗漏了哪些分支,我如何才能实现这一目标而不必运行所有单元测试并且只运行我关心的测试,
无需运行所有单元测试且仅运行我关心的测试,在回答问题时记住这些神奇的词
最佳答案
JaCoCo 不执行您的测试,它只是记录有关已执行内容的信息。因此,测试的执行,包括单个测试的情况,完全取决于您用来执行测试的工具,很遗憾,您的问题中没有提到这一点。
如果您使用 Maven 作为构建工具,那么测试的执行通常由 maven-surefire-plugin
完成和控制,它有 option test
运行个人测试。这是示例:
src/main/java/Example.java
:
public class Example {
public void doSomething(int p) {
if (p == 1) {
a();
} else {
b();
}
}
private void a() {
System.out.println("a");
}
private void b() {
System.out.println("b");
}
}
src/test/java/ExampleTest.java
:
import org.junit.Test;
public class ExampleTest {
@Test
public void test1() {
new Example().doSomething(1);
}
@Test
public void test2() {
new Example().doSomething(2);
}
}
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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
执行 mvn clean verify -Dtest=ExampleTest#test1
将在目录 target/site/jacoco
中生成以下报告:
并执行 mvn clean verify -Dtest=ExampleTest#test2
将产生:
分别显示了 test1
和 test2
的覆盖率。
为了比较 - 通过 mvn clean verify
执行所有测试产生:
关于 clean
用法的注意事项:文件 target/jacoco.exec
包含执行信息并用于生成报告(参见 agent option destfile
和 corresponding parameter of jacoco-maven-plugin
)。默认情况下,JaCoCo 代理附加到此文件(参见 agent option append
和 corresponding parameter of jacoco-maven-plugin
),因此在此示例中使用 clean
来防止在此文件中积累有关先前执行的数据。
如果你使用Gradle,那么它也会has similar ability - 给定相同的来源和 build.gradle
:
apply plugin: 'java'
apply plugin: 'jacoco'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
执行 gradle clean test --tests ExampleTest.test1 jacocoTestReport
将生成包含 test1
覆盖率的报告,这与 Maven 的情况相同。
与 Maven 的示例类似,此示例中使用 clean
来防止在文件 build/jacoco/test.exec
- see append
property of JaCoCo Gradle Plugin
中积累有关先前执行的数据。 .
如果您使用Eclipse IDE,则有EclEmma Eclipse plugin将 JaCoCo 集成到 Eclipse IDE 中,并默认包含在从 Oxygen (4.7) 版本开始的“面向 Java 开发人员的 Eclipse IDE”中。有了它,您还可以在 Eclipse IDE 中覆盖单个测试 - 在编辑器中,右键单击测试名称以获取上下文菜单并选择“Coverage As -> JUnit Test”。
关于java - Jacoco 只为单个测试类生成覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47790198/