如何在TestNG中运行多个测试类?


**testng.xml** 的格式为 **<classes>**,我们在这里定义所有应该执行的测试类。用户可以在需要执行的 **testng.xml** 中提及 n 个类。在本文中,我们将讨论如何使用单个 **testng.xml** 执行多个类。

这里,我们将有两个包含多个测试方法的类,我们将看到如何配置 **testng.xml** 来运行这两个类 - **NewTestngClass** 和 **OrderofTestExecutionInTestNG**。

解决此问题的方法/算法

  • **步骤 1** - 创建两个 TestNG 类 - NewTestngClass 和 OrderofTestExecutionInTestNG。

  • **步骤 2** - 在这两个类 - **NewTestngClass** 和 **OrderofTestExecutionInTestNG** 中编写两个不同的 @Test 方法。

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

  • **步骤 4** - 现在,运行 **testng.xml** 或直接在 IDE 中运行 TestNG 类,或者使用命令行进行编译和运行。

示例

以下代码显示了如何运行多个类:

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" /> </classes> </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年1月12日

12K+ 次浏览

启动您的职业生涯

通过完成课程获得认证

开始
广告