Maven中测试插件(surefire)的相关配置及常用方法

本文详细介绍了如何在Maven中配置和使用Surefire测试插件,包括默认执行的测试规则、跳过测试、排除特定测试、运行单个或一类测试、使用TestNG和JUnit,以及如何进行并行测试、调试测试用例、设置系统属性等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在Maven中配置测试插件surefire

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

2. 默认被执行的测试

默认情况下,surefire会执行文件名以Test开头或结尾的测试用例,或者是以TestCase结尾的测试用例。

3. 跳过测试 Skipping Tests

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
       <configuration>
          <skipTests>true</skipTests>
        </configuration></span>
      </plugin>
    </plugins>
  </build>

补充:如果使用Junit或者TestNG也可以直接在当前测试方法上加注解@Ignore忽略,这是加了该注解的也都会被skip

4. 排除测试 Exclusions (Junit & TestNG 通用)

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
         <excludes>
            <exclude>**/TestCircle.java</exclude>
            <exclude>**/TestSquare.java</exclude>
          </excludes></span>
        </configuration>
      </plugin>
    </plugins>
  </build>

5. 仅执行一个/一类测试(repeat) Running a Single Test (Junit & TestNG 通用)

在开发过程中配置命令:
mvn -Dtest=TestCircle test test表示当前测试方法所在的测试类,不需要扩展名 The value for the
test
parameter is the name of the test class
mvn -Dtest=TestCi*le test *表示任何
mvn -Dtest=TestSquare,TestCi*le test 如果测试类没有使用规范的命名,可以显示的直接指定测试方法的名称

6. 如何使用TestNG

<dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.3.1</version>
      <scope>test</scope>
    </dependency>
If  using an older version of TestNG (<= 5.11)
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>5.11</version>
      <scope>test</scope>
     <classifier>jdk15</classifier></span>
    </dependency>

默认会执行的测试用例:*Test.java

7. 如何使用TestNG的 suite

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles></span>
        </configuration>
      </plugin>

8. 执行群组测试 execute one or more specific groups (Junit & TestNG 通用)

 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
         <groups>functest,perftest</groups>
        </configuration>
      </plugin>

9. 多线程的运行测试用例 Running tests in parallel (Junit & TestNG 通用)

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
         <parallel>methods</parallel>
          <threadCount>10</threadCount>
        </configuration>
      </plugin>

10. 如何使用Junit Using JUnit

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

11. 如何使用Junit Category Using JUnit Categories

<plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.11</version>
      <configuration>
       <span style="color:#009900;"> <strong><groups>com.mycompany.SlowTests</groups></strong></span>
      </configuration>
    </plugin>

仅有带该注解的测试 或者 是当前注解 类/接口的 子类 会被执行,

12. 如何debug TestCases

mvnDebug -DforkCount=0 test   dubug非fork的测试  

13. 使用系统属性 Using System Properties

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <span style="color:#009900;"><systemPropertyVariables>
            <propertyName>propertyValue</propertyName>
            <buildDirectory>${project.build.directory}</buildDirectory>
            [...]
          </systemPropertyVariables></span>
        </configuration>
      </plugin>
    </plugins>
  </build>

14. 选择surefire provider

surefire 默认会根据工程的classpath中已有的Junit|TestNG的版本来选择 test-framework provider,我们也可以手动的选择和覆盖当前的provider

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
       <span style="color:#009900;"> <artifactId>surefire-junit47</artifactId></span>
        <version>2.17</version>
      </dependency>
    </dependencies>
  </plugin>

目前已经提供的provider有surefire-junit3, surefire-junit4, surefire-junit47 and surefire-testng

注意:选择手动指定provider时,不要忘记安装test framework

15. class Loading issues

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值