如何使用 Maven TestNG 执行失败的测试用例?


TestNG 是一个测试框架,可以使用 Maven 作为构建工具。它有助于在一个地方(pom.xml)维护依赖项及其版本。

Maven 提供了使用 surefire 插件运行的灵活性。在某些情况下,测试用例会失败,用户希望重新运行它。在这种情况下,maven 会在 target 文件夹中创建一个 testng-failed.xml 文件。最简单的方法是在第一次运行后,从命令行重新运行 xml 文件或在 pom.xml 中提及它。确保当你在 pom.xml 中提及 xml 文件时,该文件应该存在,否则它会抛出错误。

在本文中,我们将说明如何通过 maven surefire 重新运行失败的测试用例。

解决此问题的方法/算法

  • 步骤 1:创建 TestNG 类 - NewTestngClass

  • 步骤 2:在类中编写 2 个 @Test 方法,其中第二个测试失败。

  • 步骤 3:现在创建如下所示的 testNG.xml

  • 步骤 4:在 pom.xml 中添加 suiteXMLFiles 标签,并为 testng.xml 文件路径提及一个变量名称 ${xmlFilePath},如程序部分所示

  • 步骤 5:现在,使用命令行运行它,如 mvn test -DxmlFilePath=testng.xml 文件路径。

  • 步骤 6:验证 testng-failed.xml 是否已创建在 target/surefire-reports 文件夹中。

  • 步骤 7:现在重新运行此 xml 文件,方法是在 pom.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"); assert false; } }

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>

输出

[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...

Testcase 1 - executed
Testcase 2 - skip exception example
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.314 s <<< FAILURE! - in TestSuite
[ERROR] NewTestngClass.testcase2  Time elapsed: 0.008 s  <<< FAILURE!
java.lang.AssertionError
	at NewTestngClass.testcase2(NewTestngClass.java:16)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   NewTestngClass.testcase2:16
[INFO] 
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[ERROR] There are test failures.

现在,用户可以看到 testng-failed.xml 文件已创建在 target/surefire-reports 文件夹中

用户可以使用命令直接运行此文件

mvn test -DxmlFilePath=target/surefire-reports/testng-failed.xml

输出

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Testcase 2 - skip exception example
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.095 s <<< FAILURE! - in TestSuite
[ERROR] NewTestngClass.testcase2  Time elapsed: 0.017 s  <<< FAILURE!
java.lang.AssertionError
	at NewTestngClass.testcase2(NewTestngClass.java:16)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   NewTestngClass.testcase2:16
[INFO] 
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[ERROR] There are test failures. 

更新于: 2023年8月16日

633 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告