如何在 TestNG.xml 中的类路径中使用正则表达式?


testNG.xml 具有 <classes> 格式,我们在此定义应执行的所有测试类。在 <classes> 中的类中没有提供正则表达式的特定方法。但是,当然有一些解决方法,如果您想从类中运行特定的 @Test,这些方法非常有用。TestNG 在 include、excludepackage 标签中支持正则表达式。

以下是在要从测试套件运行的测试类中使用正则表达式的几种方法。

  • <classes> 内提及所有类名。并且,在类内部,使用 <methods><include name ="test.*" />。它将排除从给定类中以 test 为名称开始的所有测试。

  • 如果类存在于嵌套或不同的包中,则在 <packages><package name="common_package.*"> 中使用包名称直到公共名称,以及正则表达式。它将运行所有存在于以 common_package 开头的包名称内的类。

现在,让我们详细讨论这两种场景。

场景 1

使用正则表达式在类中运行特定方法。在这里,我们将拥有一个包含多个测试方法的类,我们将看到如何配置 testNG.xml 以仅基于正则表达式运行一些测试。我们将运行所有以“test”开头的 @Test。

解决此问题的方法/算法 -

  • 步骤 1 - 创建一个名为“NewTestngClass”的 TestNG 类。

  • 步骤 2 - 在类中编写三个不同的 @Test 方法,起始名称为 test1、test2findingAddition

  • 步骤 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"); } @Test public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } @Test public void findingAddition() { System.out.println("in finding addition of NewTestngClass"); } }

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">
      <classes>
         <class name="NewTestngClass">
            <methods>
               <include name="test.*"/>
            </methods>
         </class>
      </classes>
   </test>
</suite>

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

场景 2

使用正则表达式在类中运行特定方法。在这里,我们将拥有一个包含多个类和多个测试方法的包,我们将看到如何配置 testNG.xml 以仅基于正则表达式运行特定包。我们将运行存在于以“com”开头的包中的类。

解决此问题的方法/算法 -

  • 步骤 1 - 在 src 下创建两个包,分别为 com.testcom.work

  • 步骤 2 - 创建两个 TestNG 类:NewTestngClasscom.test 包内,OrderofTestExecutionInTestNGcom.work 包内

  • 步骤 3 - 在这两个类中编写两个不同的 @Test 方法,起始名称为 test1test2

  • 步骤 4 - 现在创建如下所示的 testNG.xml。

  • 步骤 5 - 运行 testNG.xml 或直接在 IDE 中运行 TestNG 类,或使用命令行编译并运行它。

示例

以下代码显示了如何仅从大型套件中运行 1 个测试方法 -

src/ com.test.NewTestngClass.java

import org.testng.annotations.Test; public class NewTestngClass { @Test public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } @Test public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } }

src/com.work.OrderofTestExecutionInTestNG.java -

package com.test.exclude.class; import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 1 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 2 @Test public void testCase4() { System.out.println("in test case 4 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">
   <test name = "test1">
      <packages>
         <package name="com.*">
         </package>
      </packages>
   </test>
</suite>

输出

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in test case 3 of OrderofTestExecutionInTestNG
in test case 4 of OrderofTestExecutionInTestNG
===============================================
Suite1
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
=======================

更新于: 2022-03-09

775 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告