根据昨天网上百度到的东西之中,发现了一个maven插件也能够实现生成测试覆盖率maven-surefire-plugin插件。这个插件是运用于maven生命周期的测试阶段,通过maven-surefire-plugin插件来执行Junit或者TestNG的测试用例 。可以称之为测试运行器。
使用这个插件pom.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>se.thinkcode</groupId>
<artifactId>one-module-example</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
include设置的是包含的测试源文件,在默认情况下会自动扫描符合以下要求的测试类
**/Test*.java
**/*Test.java
**/*TestCase.java
测试截图:
这个插件还能跳过测试
<configuration>
<skipTests>true</skipTests>
</configuration>
跳过某些用例
<configuration>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
这样就把所有以Test为后缀的测试类忽略掉。
测试结果:
写的唯一一个测试类,也没有被测试到。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] <<< cobertura-maven-plugin:2.7:cobertura (default-cli) < [cobertura]test @ one-module-example <<<
[INFO]
[INFO] --- cobertura-maven-plugin:2.7:cobertura (default-cli) @ one-module-example ---
[INFO] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[INFO] Cobertura: Loaded information on 1 classes.
Report time: 140ms
[INFO] Cobertura Report generation was successful.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.794 s
[INFO] Finished at: 2018-09-05T13:06:36+08:00
[INFO] Final Memory: 16M/225M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
覆盖率结果:
因为没有被执行,所以覆盖率为0。