maven-surefire-plugin

本文参考自:https://www.cnblogs.com/qyf404/p/5013694.html

surefire是maven里执行测试用例(包括testNG,Junit,pojo)的插件,他能产生两种不同形式的测试结果报告

1、纯文本 2、.xml文件格式

核心:这个插件的surefire:test命令会默认绑定maven执行的test阶段,当执行mvn test或者执行其他maven命令时跑了测试用例,那么已经用过maven-surefire-plugin了。

默认情况下,这些文件生成在工程的${basedir}/target/surefire-reports,目录下(basedir指的是pom文件所在的目录)。 

如何使用?运行测试即可,例如mvn test,mvn surefire:test,它能扫描测试类目录下的测试类,只要类名符合*Test.java,那么这个测试类就会被运行。

备注:此插件与单元测试插件配合使用,属于辅助插件。

---------------------------------------------------------------------------------------

配置

(以junit为例)

配置junit

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
</dependency>

配置maven-surefire-plugin(最简单配置)

<plugin> 
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19</version>
</plugin>

surefire会自动按一定逻辑寻找Junit插件并执行测试样例,当然也能自己指定

if the JUnit version in the project >= 4.7 and the parallel attribute has ANY value
    use junit47 provider
if JUnit >= 4.0 is present
    use junit4 provider
else
    use junit3.8.1

---------------------------------------------------------------------------------------

常见其他配置

跳过测试样例

很多指令,例如mvn package,执行时先会执行mvn test,但如果我们在打包的时候是不想执行测试样例呢?

例如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.19</version>
        </dependency>
    </dependencies>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>