如何使用 Surefire 和 TestNG 运行单个测试类或测试组?
Maven 是一个项目管理和理解工具,它提供了一个完整的构建生命周期框架。用户几乎可以立即自动化项目的构建基础设施,因为 Maven 使用标准的目录布局和默认的构建生命周期。
总而言之,Maven 简化并标准化了项目构建过程。它可以无缝地处理编译、分发、文档、团队协作和其他任务。Maven 提高了可重用性,并处理了大多数与构建相关的任务。
TestNG 是一个测试框架,可以使用 Maven 作为构建工具。它有助于在一个地方(pom.xml)维护依赖项及其版本。
用户可以使用 surefire 插件在 Maven 中运行单个类或单个测试组。当用户使用命令行时,甚至不需要 testng.xml 来运行一个类或组。
在本文中,我们将了解如何通过 surefire 运行单个 TestNG 类或测试组。
解决此问题的方法/算法
步骤 1:创建一个 TestNG 类 - NewTestngClass
步骤 2:在类中编写 2 个 @Test 方法,并指定组名。
步骤 3:现在创建如下所示的 testNG.xml。
步骤 4:不要在 pom.xml 中添加 testng.xml 的路径,如下所示。
步骤 5:不要在 pom.xml 中的 suiteXMLFiles 后添加应执行的组名,如程序部分所示。
步骤 6:现在,使用命令行运行它。
示例
以下代码演示如何仅从大型套件中运行 1 个测试方法
src/ NewTestngClass.java
import org.testng.annotations.Test; public class NewTestngClass { @Test(groups = { "group1", "group3" }) public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } @Test(groups = { "group2", "group3" }) public void testCase1() { System.out.println("in test case 2 of NewTestngClass"); } }
pom.xml
这是一个 Maven 配置文件,用于组织依赖项、插件和运行 TestNG 测试用例。
当只需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sample</groupId> <artifactId>TestNGProjectct</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> </configuration> </plugin> </plugins> </build> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng<artifactId> <version>7.3.0</version> </dependency> </dependencies> </project>
运行单个类的命令
mvn test -Dtest=NewTestngClass
输出
[INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.sample:TestNGProjectct >--------------------- [INFO] Building TestNGProjectct 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGProjectct --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGProjectct --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\Users\anandas\IdeaProjects\TestNGProjectct\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct --- [INFO] No tests to run. [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running TestSuite ... ... TestNG 7.3.0 by Cédric Beust ([email protected]) ... in test case 1 of NewTestngClass in test case 2 of NewTestngClass [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.461 s - in TestSuite [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.838 s [INFO] Finished at: 2022-02-08T16:07:17+05:30 [INFO] ------------------------------------------------------------------------
运行单个组的命令
注意 - 在运行特定测试组之前,请在 pom.xml 中添加 testng.xml 路径。
mvn test -Dgroups=group2
输出
[INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.sample:TestNGProjectct >--------------------- [INFO] Building TestNGProjectct 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGProjectct --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGProjectct --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\Users\anandas\IdeaProjects\TestNGProjectct\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct --- [INFO] No tests to run. [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running TestSuite ... ... TestNG 7.3.0 by Cédric Beust ([email protected]) ... in test case 2 of NewTestngClass [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.461 s - in TestSuite [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.838 s [INFO] Finished at: 2022-02-08T16:07:17+05:30 [INFO] ------------------------------------------------------------------------
广告