surefire插件的使用

本文详细介绍了如何利用Maven的Surefire插件进行TestNG和JUnit的测试配置,包括不同版本的JUnit配置差异,以及如何通过mvn命令灵活地运行特定测试类或方法。

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

在这里插入图片描述

写在前面

surefire 插件使用在 maven 生命周期的 test 阶段,是用来做测试使用的,他提供了一些 mvn 操作可以很方便的让我们执行我们需要的测试任务

  • 要求搭配 jdk 1.7 以上
  • 要求搭配 maven 3 以上

结合 TestNG 配置

除了配置基础的 testng 依赖之外,我们需要配置 surefire 插件

这篇官网文章专门介绍如何结合 testng 来进行配置的 http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

<plugins>
    [...]
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <!-- 版本可以自己选择 -->
        <version>RELEASE</version>
        <configuration>
            <!-- 指定运行的 xml 文件路径 -->
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
            <!-- 设置传参,可以用 @Parameters 接收 -->
            <systemPropertyVariables>
            	<propertyName>firefox</propertyName>
          </systemPropertyVariables>
          [...]
          <argLine>-Dfile.encoding=UTF-8</argLine>
        </configuration>
    </plugin>
    [...]
</plugins>

此插件中甚至可以配置 testng 的分组运行和并行运行,还有其他等多种配置,还是蛮强大的,只不过这部分我比较喜欢通过 testng.xml 中设置来控制。更加具体细节的配置请参照官网介绍使用

结合 Junit 配置

低版本 junit

除了配置基础的 junit 依赖之外,我们需要配置 surefire 插件

junit5 以下的官网:http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

<plugins>
    [...]
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-surefire-plugin</artifactId>
		<version>RELEASE</version>
		<dependencies>
			<dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-junit47</artifactId>
				<version>RELEASE</version>
			</dependency>
		</dependencies>
	</plugin>
	[...]
</plugins>

当没有配置版本时候,这里说明了 surefire 插件选用哪个版本依赖的逻辑,挺智能的

    if the JUnit 5 Platform Engine is present in the project
        use junit-platform
    if the JUnit version in the project >= 4.7 and the <<<parallel>>> configuration parameter has ANY value
        use junit47 provider
    if JUnit >= 4.0 is present
        use junit4 provider
    else
        use junit3.8.1

junit5

junit5 的 junit-jupiter 依赖包含了 junit-jupiter-engine 然后 junit-jupiter-engine 中又包含了 junit-platform-launcher

官网介绍使用:http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

<plugins>
    [...]
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>RELEASE</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.3.2</version>
            </dependency>
        </dependencies>
    </plugin>
    [...]
</plugins>

当没有配置时候尊崇这样的逻辑,还是很智能的

    if the JUnit 5 Platform Engine is present in the project
        use junit-platform
    if the JUnit version in the project >= 4.7 and the <<<parallel>>> configuration parameter has ANY value
        use junit47 provider
    if JUnit >= 4.0 is present
        use junit4 provider
    else
        use junit3.8.1

其余的使用请参见官网介绍

配套 mvn 命令使用

官网也有介绍使用 http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

# 测试单个测试类
mvn -Dtest=TestCircle test
mvn -Dtest=TestCi*le test

# 测试具体的方法
mvn -Dtest=TestCircle#mytest test
mvn -Dtest=TestCircle#test* test
# Since of Surefire Plugin 2.19 you can select multiple methods (JUnit 4, JUnit 4.7+ and TestNG):
mvn -Dtest=TestCircle#testOne+testTwo test

甚至支持重跑失败的测试和 debug 测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

abcnull

您的打赏是我创作的动力之一

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值