如何使用 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.