surefire:surefire插件的使用 2024-04-04 18:26:46 0 0 文章目录写在前面结合 TestNG 配置结合 Junit 配置配套 mvn 命令使用 写在前面 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 testmvn -Dtest=TestCi*le test# 测试具体的方法mvn -Dtest=TestCircle#mytest testmvn -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 测试 广告 实用主义学Python(小白也容易上手的Python实用案例) 收藏(0)