如何通过Maven运行特定的TestNG测试组?
Maven是一个项目管理和理解工具,它提供了一个完整的构建生命周期框架。用户几乎可以立即自动化项目的构建基础设施,因为Maven使用标准的目录布局和默认的构建生命周期。
总而言之,Maven简化和标准化了项目构建过程。它可以无缝地处理编译、分发、文档、团队协作和其他任务。Maven提高了可重用性,并负责处理大多数与构建相关的任务。
TestNG是一个测试框架,可以使用Maven作为构建工具。它有助于在一个地方(pom.xml)维护依赖项及其版本。
用户可以从testng.xml或pom.xml运行测试。要从pom.xml运行测试,用户需要提及testng.xml的路径,并需要maven-surefire-plugin来执行。除了testng.xml,Maven还提供了通过Maven或命令行运行特定组的功能。通过命令行运行时,用户可以在运行时提及组名,而无需更改testng.xml或pom.xml中的任何内容。
在本文中,我们将了解如何通过Maven运行特定的TestNG测试组。
场景1
在这个场景中,我们将说明如何在pom.xml中传递组名以运行特定的测试组,而无需更改testng.xml中的任何内容。
解决此问题的步骤/算法
步骤1:创建TestNG类 - NewTestngClass
步骤2:在类中编写2个@Test方法,并指定组名。
步骤3:现在创建如下所示的testng.xml。
步骤4:在pom.xml中添加testng.xml的路径,如下所示。
<configuration>
<suiteXmlFiles>
<suiteXmlFile>Path of testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
在配置部分,提到了testng的路径。需要记住两点,第一,此配置应与maven-surefire-plugin一起保留;第二,切勿忘记添加testng的依赖项。详细的pom.xml可以在程序部分找到。
步骤5:在pom.xml中,在suiteXMLFiles之后添加应执行的组名,如程序部分所示。在这里,我们将只运行具有group2组名的@Test。
步骤6:现在,在IDE中运行pom.xml,或者使用命令行编译并运行它。
示例
以下代码演示了如何仅从大型套件中运行一个测试方法。
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");
}
}
testng.xml
这是一个用于组织和运行TestNG测试用例的配置文件。
当只需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
<test name = "test1" preserve-order = "true">
<classes>
<class name=”NewTestngClass”/>
</classes>
</test>
</suite>
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>
<suiteXmlFiles>
<suiteXmlFile>src/main/java/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<groups>group2</groups>
</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>
输出
[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 (cedric@beust.com) ... 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] ------------------------------------------------------------------------
场景2
在这个场景中,我们将说明如何在命令行中传递组名以运行特定的测试组,而无需更改testng.xml或pom.xml中的任何内容。
解决此问题的步骤/算法
步骤1:创建TestNG类 - NewTestngClass
步骤2:在类中编写2个@Test方法,并指定组名。
步骤3:现在创建如下所示的testng.xml
步骤4:在pom.xml中添加testng.xml的路径,如下所示。
在配置部分,提到了testng的路径。需要记住两点,第一,此配置应与maven-surefire-plugin一起保留;第二,切勿忘记添加testng的依赖项。
详细的pom.xml可以在程序部分找到。
步骤5:我们不会在pom.xml或testng.xml中添加任何组名。
步骤6:现在,使用命令行运行它。
示例
以下代码演示了如何仅从大型套件中运行一个测试方法。
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");
}
}
testng.xml
这是一个用于组织和运行TestNG测试用例的配置文件。
当只需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
<test name = "test1" preserve-order = "true">
<classes>
<class name=”NewTestngClass”/>
</classes>
</test>
</suite>
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>
<suiteXmlFiles>
<suiteXmlFile>src/main/java/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</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 -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 (cedric@beust.com) ... 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] ------------------------------------------------------------------------
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP