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