TestNG 如何包含两个组名并创建一个组来运行测试
TestNG支持根据相似功能或用途将测试用例分组。然而,用户可以将多个组添加到单个测试中。当使用<groups><run><include>
语法运行组时,TestNG运行所有属于该组的测试。它起到OR语句的作用。例如,如果一个测试有2个组,但只在<include>
标签中提到了1个组,它将运行测试。
然而,当用户希望仅当所有组都被提及时才运行测试时,即AND语句,TestNG并不直接支持AND语句。例如:@Test(groups={“unit”, “integration”})
如果用户只想在组名为unit和integration而不是unit或integration时运行此测试。
这个功能可以通过Beanshell来支持。它为用户提供了在testng.xml中根据需要放置自定义条件的脚本化工具。
解决上述问题的方法可以是Beanshell脚本。用户可以在testng.xml中使用
步骤
- 步骤1:创建一个TestNG类 – NewTestngClass
- 步骤2:在所有类中编写3个带有2个作为单独组的@Test方法,而其中一个方法具有2个组。
- 步骤3:现在根据下面给出的Beanshell脚本创建testNG.xml。
- 步骤4:如果您仅运行testng.xml(而不是与maven一起),请确保已下载并正确配置了Beanshell jar。
- 步骤5:如果您将testng.xml作为maven构建的一部分运行,请在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
这是一个用于组织依赖项、插件和运行TestNG测试用例的Maven配置文件。
当只需要执行有限的测试而不是整个套件时,它非常方便。
<?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] --------------------------------------------------------