如何使用批處理檔案執行Maven專案的testng.xml檔案?
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 clean test -DxmlFilePath=<testng.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");
}
@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>
批處理檔案
編寫以下命令並另存為.bat檔案
set projectLocation=C:\locationOfYourProject\locationOfYourTestNGXmlFile cd %projectLocation% mvn clean 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