如何在 testng.xml 中通过通配符使用 TestNG 运行类?

testngrest assureddynamic programming更新于 2025/5/6 4:22:17

testng.xml 的格式为 <classes>,我们在其中定义应执行哪些测试类。在 <classes> 中的类中没有任何特定方法提供正则表达式。但是有一些变通方法可用于从类运行特定的 @Test。TestNG 在 include、exclude 和 package 标签中支持正则表达式。

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

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

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

在本文中,我们将讨论 #1 和 #2。

场景 1

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

解决此问题的方法/算法

  • 步骤 1:创建一个 TestNG 类 - NewTestngClass。

  • 步骤 2:在类中编写 3 个不同的 @Test 方法 - NewTestngClass,名称分别为 test1、test2 和 findingAddition。

  • 步骤 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>

输出

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 下创建 2 个包,分别为 com.test 和 com.work

  • 步骤 2:创建 2 个 TestNG 类 - 包 com.test 内的 NewTestngClass 和包 com.work 内的 OrderofTestExecutionInTestNG

  • 步骤 3:在两个类中编写 2 个不同的 @Test 方法 - 名称分别为 test1 和 test2。

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

  • 步骤 4:现在,运行 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
=======================


相关文章