如何在TestNG中使用Beanshell指令碼?
TestNG支援基於相似功能或用途對測試用例進行分組。
有時,使用者需要根據條件和用例在執行時自定義選擇類/方法/組。TestNG支援簡單的常用場景,但涵蓋所有預期是不必要的。例如,使用者可以向單個測試新增多個組。使用`
但是,如果使用者只想在提及所有組(即AND語句)時執行測試。TestNG不直接支援組中的AND語句。例如:@Test ( groups = {“unit”, “integration”} )
如果使用者只想在組被同時指定為“unit”和“integration”(而不是“unit”或“integration”)時執行此測試。
此功能可以透過Beanshell實現。它為使用者提供了指令碼功能,以便根據需要在testng.xml中設定自定義條件。使用者可以新增任何條件作為指令碼,以獲取所需的測試/方法/類/組或任何其他內容。
在本教程中,我們將說明如何在testng.xml中實現Beanshell以實現自定義條件。示例針對多個組條件。
解決此問題的方法/演算法
步驟1:建立TestNG類 - NewTestngClass
步驟2:在所有類中編寫3個@Test方法,其中兩個為單一組,一個具有兩個組。
步驟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