如何使用testng.xml从测试套件中排除一个测试类?


testng.xml 的格式为 <classes>,我们在这里定义所有应该执行的测试类。在 <classes> 中没有专门排除类的方法,但是如果不想在测试套件中运行特定的类,有一些非常有用的变通方法。

以下是从测试套件中排除测试类运行的一些便捷方法。

  • 像往常一样,只需提及需要执行的类名,并删除不需要执行的类名。

  • <classes> 内提及所有类名,包括不应该运行的类名。并且,在应该排除的类内,使用 <methods><exclude name =.* />。这将排除此类中的所有测试,并运行所有其他类。

  • 如果类存在于嵌套的或不同的包中,则在 <packages><package name=common_package><exclude name= common_package.exclude_package_name /> 中使用包名称直到公共名称。这将排除包中提到的类名。但是,如果同一个包中有多个文件,则上述方法很有用。

在本文中,我们将讨论如何实现最后两种方法。

场景1

排除类中的所有方法以从套件中排除类执行。这里,我们将有两个包含多个测试方法的类,我们将看到如何配置 testng.xml 以仅运行一个类“NewTestngClass”并排除另一个类。

解决此问题的方法/算法

  • 步骤1 - 创建两个 TestNG 类:NewTestngClassOrderofTestExecutionInTestNG

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

src/OrderofTestExecutionInTestNG.java

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">
      <classes>
         <class name = "NewTestngClass" />
         <class name = "OrderofTestExecutionInTestNG" >
            <methods>
               <exclude name=".*" />
            </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

如果类存在于嵌套的或不同的包中,则使用标签 <packages> 从测试套件中排除测试类。

包名应采用 common_packages.excluding_class_package 的格式。这里,我们将有两个包含多个测试方法的类,我们将看到如何配置 testng.xml 以仅运行一个类“NewTestngClass”并排除另一个类。

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

  • 步骤1 - 在 src 下创建一个名为 com.test 的公共包。

  • 步骤2 - 在 com.test 内创建另一个名为 exclude.class 的包。

  • 步骤3 - 创建两个 TestNG 类:NewTestngClasscom.test 包中,OrderofTestExecutionInTestNGexclude.class 包中。

  • 步骤4 - 在这两个类中编写两种不同的 @Test 方法:NewTestngClassOrderofTestExecutionInTestNG

  • 步骤5 - 现在创建如下所示的 testng.xml

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

示例

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

src/ com.test.NewTestngClass.java

package com.test; 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.test.exclude.class.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.test.*">
            <exclude name="com.test.exclude.class" />
         </package>
      </packages>
   </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
===============================================

更新于:2022年3月9日

5K+ 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告