🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# TestNG 并行执行测试,类和套件 > 原文: [https://howtodoinjava.com/testng/testng-executing-parallel-tests/](https://howtodoinjava.com/testng/testng-executing-parallel-tests/) [TestNG](https://testng.org/) **并行执行**测试,类和套件以及示例。 了解如何在多个线程中并行或单个运行 testng 测试和套件。 在软件方面,并行性或多线程定义为软件,操作系统或程序同时执行另一个程序的多个部分或子组件的能力。 TestNG 允许测试以并行或多线程模式运行。 这意味着基于测试套件的配置,不同的线程将同时启动,并在其中执行测试方法。 与正常执行相比,这给用户带来了很多优势,主要是减少了执行时间和验证多线程代码的能力。 ```java Table Of Contents 1\. Advantages of parallel rests execution 2\. Run parallel testcases 3\. Run test classes in parallel 4\. Run tests suite in parallel 5\. Configure a testcase to run in multiple threads ``` ## 1\. 并行执行测试的优点 并行或多线程执行可以为用户提供很多优势。 以下是两个: 1. **减少了执行时间** – 并行执行测试时,会同时执行多个测试,因此减少了执行测试所需的总时间。 2. **允许多线程测试** – 使用此功能,我们可以编写测试来验证应用中的某些多线程代码。 *并行测试执行*被 QA 行业广泛用于功能自动化测试。 此功能可帮助质量检查人员配置其测试,以使其易于同时在多个浏览器或操作系统中执行。 在 TestNG 中可以使用多种不同的方式配置并行功能。 ## 2\. 并行运行 testng 测试用例 TestNG 提供了多种方法**在多线程条件**中执行测试,其中一种方法是在单个线程中执行每种测试方法。 此模式显着减少了执行时间,因为可以并行执行更多测试,从而减少了总执行时间。 testng 中用于**并行测试执行**的 Java 程序。 这是一个如何使用单个类文件中的`testng xml`并行运行方法的示例。 ```java package com.howtodoinjava.parallelism; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class ParallelMethodTest { @BeforeMethod public void beforeMethod() { long id = Thread.currentThread().getId(); System.out.println("Before test-method. Thread id is: " + id); } @Test public void testMethodsOne() { long id = Thread.currentThread().getId(); System.out.println("Simple test-method One. Thread id is: " + id); } @Test public void testMethodsTwo() { long id = Thread.currentThread().getId(); System.out.println("Simple test-method Two. Thread id is: " + id); } @AfterMethod public void afterMethod() { long id = Thread.currentThread().getId(); System.out.println("After test-method. Thread id is: " + id); } } ``` 前面的测试类包含两个测试方法,它们在执行时将消息打印到控制台上。 使用`Thread.currentThread.getId()`代码求值正在执行当前方法的线程的 ID。 它还包含`before`和`after`方法,它们在执行时还将当前线程的线程 ID 打印到控制台上。 在项目下创建一个名为`methods-test-testng.xml`的新文件,并编写以下代码。 ```java <suite name="Test-method Suite" parallel="methods" thread-count="2" > <test name="Test-method test" group-by-instances="true"> <classes> <class name="com.howtodoinjava.parallelism.ParallelMethodTest" /> </classes> </test> </suite> ``` 在 Eclipse 中选择此文件并将其作为 TestNG 套件运行。 您将在“控制台”窗口中看到以下测试结果: ```java Before test-method. Thread id is: 10 Before test-method. Thread id is: 9 Simple test-method Two. Thread id is: 10 Simple test-method One. Thread id is: 9 After test-method. Thread id is: 10 After test-method. Thread id is: 9 ``` 注意,上一个屏幕截图中显示的`Id`值在控制台输出中可能会不同。 `Id`值是在执行期间由 Java 虚拟机(JVM)在运行时分配的。 先前的测试结果清楚地表明,每种测试方法及其各自的`before`和`after`方法都是在不同的线程中执行的。 这由控制台上打印的线程的 ID 标识。 ## 2\. 并行运行 testng 测试类 在此示例中,我们将了解并行执行 testng 测试类; 属于测试执行的每个测试类都将在其自己的线程中执行。 ```java public class ParallelClassesTestOne { @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("Before test-class. Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method One. Thread id is: " + id); } @Test public void testMethodTwo() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method Two. Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("After test-class. Thread id is: " + id); } } ``` ```java public class ParallelClassesTestTwo { @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("Before test-class. Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method One. Thread id is: " + id); } @Test public void testMethodTwo() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method Two. Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("After test-class. Thread id is: " + id); } } ``` 在项目下创建一个名为`classes-test-testng.xml`的新文件,并编写以下代码。 ```java <suite name="Test-class Suite" parallel="classes" thread-count="2" > <test name="Test-class test" > <classes> <class name="com.howtodoinjava.parallelism.ParallelClassesTestOne" /> <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" /> </classes> </test> </suite> ``` 在 Eclipse 中选择此文件并将其作为 TestNG 套件运行。 您将在“控制台”窗口中看到以下测试结果: ```java Before test-class. Thread id is: 10 Before test-class. Thread id is: 9 Sample test-method One. Thread id is: 9 Sample test-method One. Thread id is: 10 Sample test-method Two. Thread id is: 10 After test-class. Thread id is: 10 Sample test-method Two. Thread id is: 9 After test-class. Thread id is: 9 ``` 先前的测试结果清楚地表明,每个测试类及其各自的`beforeClass`和`afterClass`方法都是在不同的线程中执行的。 这由控制台上打印的线程的 ID 标识。 ## 4\. 并行运行 TestNG 套件 让我们学习一下**在套件**中并行执行每个测试,也就是说,作为测试套件执行一部分的每个测试都将在各自独立的线程中执行。 ```java package com.howtodoinjava.parallelism; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParallelSuiteTest { String testName = ""; @BeforeTest @Parameters({ "test-name" }) public void beforeTest(String testName) { this.testName = testName; long id = Thread.currentThread().getId(); System.out.println("Before test " + testName + ". Thread id is: " + id); } @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("Before test-class " + testName + ". Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method " + testName + ". Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("After test-method " + testName + ". Thread id is: " + id); } @AfterTest public void afterTest() { long id = Thread.currentThread().getId(); System.out.println("After test " + testName + ". Thread id is: " + id); } } ``` 在项目下创建一个名为`suite-test-testng.xml`的新文件,并编写以下代码。 ```java <suite name="Test-class Suite" parallel="tests" thread-count="2"> <test name="Test-class test 1"> <parameter name="test-name" value="test-method One" /> <classes> <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" /> </classes> </test> <test name="Test-class test 2"> <parameter name="test-name" value="test-method One" /> <classes> <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" /> </classes> </test> </suite> ``` 在 Eclipse 中选择此文件并将其作为 TestNG 套件运行。 您将在“控制台”窗口中看到以下测试结果: ```java Before test Test One. Thread id is: 9 Before test Test Two. Thread id is: 10 Before test-class Test One. Thread id is: 9 Before test-class Test Two. Thread id is: 10 Sample test-method Test One. Thread id is: 9 Sample test-method Test Two. Thread id is: 10 After test-method Test Two. Thread id is: 10 After test-method Test One. Thread id is: 9 After test Test One. Thread id is: 9 After test Test Two. Thread id is: 10 ``` 先前的测试结果清楚地表明,套件中的每个测试都在其各自的线程中执行。 这由控制台上打印的线程的`ID`标识。 ## 5\. 配置 testng 测试以在多个线程中运行 前面我们讨论了如何以并行或多线程模式运行类,方法和测试。 TestNG 还提供了灵活性,可以**配置要在多线程环境**中运行的测试方法。 这是通过在方法上使用`@Test`注解时对其进行配置来实现的。 ```java public class IndependentTest { @Test(threadPoolSize = 3, invocationCount = 6, timeOut = 1000) public void testMethod() { Long id = Thread.currentThread().getId(); System.out.println("Test method executing on thread with id: " + id); } } ``` 通过使用`threadPoolSize`属性以及“测试”注解,该方法被配置为在多线程模式下运行。 `threadPoolSize`的值设置为 3; 这将测试方法配置为在三个不同的线程中运行。 其他两个属性`invocationCount`和`timeOut`,将测试配置为多次调用,并且如果执行花费很多,则失败 更多时间。 在项目下创建一个名为`independent-test-testng.xml`的新文件,并编写以下代码。 ```java <suite name="Independent test Suite" > <test name="Independent test"> <classes> <class name="com.howtodoinjava.parallelism.IndependentTest" /> </classes> </test> </suite> ``` 在 Eclipse 中选择此文件并将其作为 TestNG 套件运行。 您将在“控制台”窗口中看到以下测试结果: ```java Test method executing on thread with id: 11 Test method executing on thread with id: 10 Test method executing on thread with id: 9 Test method executing on thread with id: 11 Test method executing on thread with id: 11 Test method executing on thread with id: 10 ``` 在此,根据`invocationCount`属性值多次执行测试方法。 每次执行均在单独的线程中完成,该线程从测试报告输出中清晰可见。 当您只想在多线程模式下而不是整个测试套件中运行固定数量的测试方法时,此功能很有用。 学习愉快!