如何在Maven的pom.xml檔案中呼叫testng.xml檔案?
Maven是一個專案管理和理解工具,它提供了一個完整的構建生命週期框架。使用者幾乎可以立即自動化專案的構建基礎設施,因為Maven使用標準的目錄佈局和預設的構建生命週期。
在多環境的情況下,Maven可以很快地設定按照標準工作的方式。由於大多數專案設定都很簡單且可重用,因此Maven在建立報表、檢查、構建和測試自動化設定時使生活更輕鬆。
Maven為開發人員提供了管理以下內容的方法:
構建
文件
報表
依賴項
SCM(軟體配置管理)
釋出
分發
郵件列表
總而言之,Maven簡化並標準化了專案構建過程。它可以無縫地處理編譯、分發、文件、團隊協作和其他任務。Maven提高了可重用性,並負責處理大多數與構建相關的任務。
TestNG是一個測試框架,可以使用Maven作為構建工具。它有助於在一個地方(pom.xml)維護依賴項及其版本。
使用者可以從testng.xml或pom.xml執行測試。要從pom.xml執行測試,使用者需要提及testng.xml的路徑,並需要maven-surefire-plugin來執行。
在本文中,我們將瞭解在哪裡提及pom.xml中的testng.xml細節以及格式。
解決此問題的方法/演算法
步驟1:建立TestNG類 - NewTestngClass
步驟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的依賴項。
步驟5:現在,在IDE中執行pom.xml,或者使用命令列編譯並執行它。
示例
以下程式碼演示如何從大型套件中執行單個測試方法
src/ NewTestngClass.java
import org.testng.annotations.Test; public class NewTestngClass { @Test public void testCase1() { System.out.println("in test case 1 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>
輸出
4.0.0 com.sample TestNGProjectct 1.0-SNAPSHOT org.apache.maven.plugins maven-surefire-plugin 3.0.0-M5 src/main/java/testng.xml