如何在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