如何在TestNG中包含兩個組名並建立一個組來執行測試?
TestNG 支援基於相似功能或用途對測試用例進行分組。但是,使用者可以向單個測試新增多個組。使用 <groups><run><include> 語法執行組時,TestNG 將執行屬於該組的所有測試。它作為 OR 語句工作。例如,如果一個測試有兩個組,並且 <include> 標籤中只提及了一個組,它將執行該測試。
但是,當用戶只想在提及所有組時執行測試(即 AND 語句)時,TestNG 不直接支援組中的 AND 語句。例如:@Test (groups = {“unit”, “integration”} )
如果使用者只想在組被提及為 “unit” 和 “integration”(而不是 “unit” 或 “integration”)時執行此測試。
此功能可以透過 Beanshell 支援。它為使用者提供指令碼功能,以便根據需要在 testng.xml 中設定自定義條件。
上述問題的解決方案可以在 Beanshell 指令碼中實現。使用者可以使用 <method−selectors> 標籤而不是 <groups> 標籤在 testng.xml 中提供簡單的程式碼。它將在執行時評估組並獲取應執行的測試。
解決此問題的方法/演算法
步驟 1:建立 TestNG 類 - NewTestngClass
步驟 2:在所有類中編寫 3 個 @Test 方法,其中 2 個為單組,一個具有 2 個組。
步驟 3:現在建立如下所示帶有 Beanshell 指令碼的 testNG.xml。
步驟 4:如果您單獨執行 testng.xml(不使用 maven),請確保已下載並正確配置 Beanshell jar。
步驟 5:如果您正在執行作為 maven 構建一部分的 testng.xml,請在 pom.xml 中新增 beanshell 依賴項。
步驟 6:現在,直接執行 testNG.xml 或使用 mvn 命令。
示例
以下程式碼展示瞭如何僅基於自定義條件執行測試組
src/ NewTestngClass.java
import org.testng.annotations.Test;
public class NewTestngClass {
@Test(groups = { "unit", "integration" })
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
@Test(groups = { "integration" })
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
}
@Test(groups = { "unit" })
public void testCase3() {
System.out.println("in test case 3 of NewTestngClass");
}
}
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">
<method-selectors>
<method-selector>
<script language="beanshell"><![CDATA[
return groups.containsKey("unit") && groups.containsKey("integration");
]]></script>
</method-selector>
</method-selectors>
<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>
<dependency>
<groupId>org.apache-extras.beanshell</groupId>
<artifactId>bsh</artifactId>
<version>2.0b6</version>
</dependency>
</dependencies>
</project>
輸出
[INFO] ------------< com.sample:TestNGProjectct >------------ [INFO] Building TestNGProjectct 1.0-SNAPSHOT [INFO] -----------------------[ jar ]------------------------ [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] Nothing to compile - all classes are up to date [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 3 source files 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 NewTestngClass [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.625 s - in TestSuite [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] -------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------- [INFO] Total time: 3.222 s [INFO] Finished at: 2022-02-16T11:27:08+05:30 [INFO] --------------------------------------------------------
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP