Jacoco 仅为单个测试生成覆盖率报告 class
Jacoco generate coverage report for only a single test class
所以假设我有一个测试
@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
and corresponding parameter of jacoco-maven-plugin
). By default JaCoCo agent appends to this files ( see agent option append
and 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
.
中积累有关先前执行的数据
如果你使用EclipseIDE,那么有EclEmma Eclipse plugin在EclipseIDE中集成了JaCoCo,默认包含在"Eclipse IDE for Java Developers" 从 Oxygen (4.7) 版本开始。有了它,您还可以在 Eclipse IDE 中覆盖单个测试 - 在编辑器中右键单击测试名称以获取上下文菜单和 select "Coverage As -> JUnit Test".
所以假设我有一个测试
@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
and corresponding parameter of jacoco-maven-plugin
). By default JaCoCo agent appends to this files ( see agent option append
and 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
.
如果你使用EclipseIDE,那么有EclEmma Eclipse plugin在EclipseIDE中集成了JaCoCo,默认包含在"Eclipse IDE for Java Developers" 从 Oxygen (4.7) 版本开始。有了它,您还可以在 Eclipse IDE 中覆盖单个测试 - 在编辑器中右键单击测试名称以获取上下文菜单和 select "Coverage As -> JUnit Test".