surefire插件的使用

@[toc]

在这里插入图片描述

写在前面

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

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

结合 TestNG 配置

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

这篇官网文章专门介绍如何结合 testng 来进行配置的 maven.apache.org/surefire/ma…

<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 以下的官网:maven.apache.org/surefire/ma…

<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

官网介绍使用:maven.apache.org/surefire/ma…

<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 命令使用

官网也有介绍使用 maven.apache.org/surefire/ma…

# 测试单个测试类
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 测试


广告

[video(video-RgKslFQl-1591943416447)(type-edu_course)(url-edu.csdn.net/course/blog…)]
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享