如何使用Maven命令行运行特定的TestNG套件?
TestNG是一个测试框架,可以使用Maven作为构建工具。它有助于在一个pom.xml文件中维护依赖项及其版本。
Maven提供了使用surefire插件运行测试的灵活性。如果用户有多个testng.xml文件(请注意,一个testng文件只包含一个测试套件),他/她可以根据需要运行特定的套件。Maven允许将suiteXMLFiles定义为变量,并使用命令行在运行时传递变量的值。
在本文中,我们将说明如何使用Maven命令行运行特定的TestNG套件。
解决此问题的方法/算法
步骤1:创建一个TestNG类 - NewTestngClass
步骤2:在类中编写2个@Test方法。
步骤3:现在创建如下所示的testng.xml。
步骤4:在pom.xml中添加suiteXMLFiles标签,并为testng.xml文件路径指定一个变量名${xmlFilePath},如程序部分所示。
步骤5:现在,使用命令行运行它:mvn test -DxmlFilePath=<testng.xml 文件路径>。
示例
以下代码演示如何仅从大型套件中运行1个测试方法
src/ NewTestngClass.java
import org.testng.annotations.Test;
public class NewTestngClass {
@Test()
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
@Test(groups = { "group2", "group3" })
public void testCase2() {
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>
<suiteXmlFiles> <suiteXmlFile>${xmlFilePath}</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 -DxmlFilePath=src/main/java/testng.xml
输出
[INFO] Running TestSuite ... ... TestNG 7.3.0 by Cédric Beust (cedric@beust.com) ... in test case 1 of NewTestngClass in test case 2 of NewTestngClass [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.314 s
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP