如何使用 Maven 從命令列向 testng.xml 傳遞引數?
TestNG 是一個測試框架,可以使用 Maven 作為構建工具。它有助於在一個地方(pom.xml)維護依賴項及其版本。
Maven 提供了使用 surefire 外掛執行的靈活性。Surefire 外掛具有從命令列向 testng.xml 傳遞引數的功能。為了實現這一點,使用者必須在 testng.xml 中定義引數並設定預設值。在執行時,使用者必須傳送引數值以覆蓋預設值。透過這種方式,命令列中傳遞的值將優先,TestNG 類將使用它。如果未從命令列傳遞值,則使用預設值。
在 TestNG 類中訪問引數的另一種方法是使用 System.getProperty(“propertyName”)。在 Maven 的命令列中傳送的引數將儲存為系統屬性,並且可以在程式碼的任何地方使用。
在本教程中,我們將說明如何使用 Maven 從命令列向 testng.xml 傳遞引數。
解決此問題的方法/演算法
步驟 1:建立 TestNG 類 - NewTest
步驟 2:在類中編寫 2 個 @Test 方法。
步驟 3:在 pom.xml 檔案中提及 testng.xml 路徑。請確保 pom.xml 中未提及 systemPropertyVariables。
步驟 4:編寫邏輯或引數化程式碼以訪問此變數。
步驟 5:建立 testng.xml 並提及需要透過命令列傳送的引數,如程式部分所示。
步驟 6:現在在執行時在命令中傳遞變數的值。
mvn test -Denvironment=UAT
步驟 7:現在,驗證是否訪問了相同的變數值。
示例
以下程式碼展示瞭如何從大型套件中執行單個測試方法。
src/test/java/NewTest.java
import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class NewTest { @Test() public void testCase1() { System.out.println("in test case 1 of NewTest"); System.out.println("Regression " + System.getProperty("environment")); } @Parameters("environment") @Test() public void testCase2(String environment) { System.out.println("in test case 2 of NewTest"); System.out.println("Environment name is- "+environment); } }
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>
testng.xml
這是一個配置檔案,用於組織和執行 TestNG 測試用例。
當只需要執行有限的測試而不是完整套件時,它非常方便。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="suite" parallel="none" verbose="5"> <test name="test"> <parameter name="environment" value="QA"/> <classes> <class name="NewTest"/> </classes> </test> </suite>
執行特定套件的命令
請注意,如果需要排除多個套件,只需用逗號分隔所有套件名稱。
mvn clean test -Denvironment=UAT
輸出
[INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.sample:TestNGProjectct >--------------------- [INFO] Building TestNGProjectct 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGProjectct --- [INFO] Deleting C:\Users\anandas\IdeaProjects\TestNGProjectct\target [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 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 8 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\classes [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] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct --- [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 1 of NewTest Regression UAT in test case 2 of NewTest Environment name is- UAT [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.619 s - in TestSuite [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] -------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------- [INFO] Total time: 3.443 s [INFO] Finished at: 2022-02-11T10:52:24+05:30 [INFO] -------------------------------------------------------- Process finished with exit code 0