如何在testng.xml中通过名称和通配符运行TestNG类?
testng.xml 的格式为 <classes>,我们在此定义所有要执行的测试类。在 <classes> 中的类中没有提供正则表达式的特定方法。但是,有一些解决方法可用于运行类中的特定 @Test。TestNG 支持在 include、exclude 和 package 标签中的正则表达式。
这里,问题陈述是当用户只想运行名称格式相同的特定类时,例如类的初始名称应相同。例如,用户希望运行所有名称以 NewTest 开头的类。
在本教程中,我们将讨论如何运行所有名称以 NewTest 开头的类。
上述问题的解决方案可以在 beanshell 脚本中实现。用户可以使用 <method−selectors> 标签而不是 <classes> 在 testng.xml 中提供简单的代码。它将在运行时进行评估,并获取应运行的类名。
解决此问题的方法/算法
步骤 1:创建 3 个 TestNG 类 - NewTestngClass、NewTestNGClass1 和 OrderofTestExecutionInTestNG。
步骤 2:在所有类中编写 @Test 方法。
步骤 3:现在创建如下所示的 testNG.xml。
步骤 4:现在,运行 testNG.xml 或直接在 IDE 中运行 TestNG 类,或者使用命令行进行编译和运行。
示例
以下代码演示如何仅从大型套件中运行 1 个测试方法
src/ NewTestngClass.java
import org.testng.annotations.Test;
public class NewTestngClass {
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
}
src/ NewTestngClass1.java
import org.testng.annotations.Test;
public class NewTestngClass {
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass1");
}
}
src/ NewTestngClass.java
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
@Test
public void testCase1() {
System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
}
}
testng.xml
这是一个用于组织和运行 TestNG 测试用例的配置文件。
当只需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
<test name = "test1" preserve-order = "true">
<method-selectors>
<method-selector>
<script language="beanshell"><![CDATA[
method.getDeclaringClass().getSimpleName().startsWith("NewTest")
]]>
</script>
</method-selector>
</method-selectors>
</test>
</suite>
输出
in test case 1 of NewTestngClass in test case 1 of NewTestngClass1 =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP