TestNG 中 ParallelMode.METHODS,ParallelMode.CLASSES,ParallelMode.TESTS区别

本文介绍了TestNG中ParallelMode的不同选项及其对测试用例执行的影响。通过实例对比了METHODS、CLASSES和TESTS三种模式下线程分配的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 TestNG 中,可以设置testNG 是否是多线程的,这时候 需要用到ParallelMode来定义
那么ParallelMode.METHODS,ParallelMode.CLASSES, ParallelMode.TESTS 有什么区别呢?

结论:
ParallelMode.METHODS:一个线程负责一个@Test标签的程序,多个并行时,每个class,method之间的线程ID都不一样
ParallelMode.CLASSES:一个线程 负责一个class里面所有的带有@Test标签的method,多个并行时,每个class之间的线程id不一样,但是同一个class里的@Test的method线程id是一样的
ParallelMode.TESTS:一个线程负责一个TestNG.xml中的一个< Test >标签,多个并行时,每个 < Test > 所包含的class,method之间的线程ID 是一致的,但是 每个< Test > 之间的线程ID是不一致的

做个实验:
ParallelMode.METHODS:

package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ParallelClassesTestTwo {
    @BeforeClass
    public void beforeClass() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Before test-class. Thread id is: " + id);
    }

    @BeforeMethod
    public void beforeMethod() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Before test-method. Thread id is: " + id);
    }

    @Test
    public void testMethodOne() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Sample test-method One. Thread id is: " + id);
    }

    @Test
    public void testMethodTwo() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Sample test-method Two. Thread id is: " + id);
    }

    @AfterMethod
    public void afterMethod() {
        long id = Thread.currentThread().getId();
        System.out.println("222 After test-method. Thread id is: " + id);
    }

    @AfterClass
    public void afterClass() {
        long id = Thread.currentThread().getId();
        System.out.println("222 After test-class. Thread id is: " + id);
    }
}

对应的testNG.xml 如下,

<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.ParallelClassesTestTwo" />

        </classes>
    </test>
</suite>

得到的结果为:

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

222 Before test-class. Thread id is: 11
222 Before test-method. Thread id is: 10
222 Before test-method. Thread id is: 11
222 Sample test-method Two. Thread id is: 11
222 Sample test-method One. Thread id is: 10
222 After test-method. Thread id is: 10
222 After test-method. Thread id is: 11
222 After test-class. Thread id is: 11

===============================================
Test-method Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

ParallelMode.CLASSES:

package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ParallelClassesTestTwo {
    @BeforeClass
    public void beforeClass() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Before test-class. Thread id is: " + id);
    }

    @BeforeMethod
    public void beforeMethod() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Before test-method. Thread id is: " + id);
    }

    @Test
    public void testMethodOne() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Sample test-method One. Thread id is: " + id);
    }

    @Test
    public void testMethodTwo() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Sample test-method Two. Thread id is: " + id);
    }

    @AfterMethod
    public void afterMethod() {
        long id = Thread.currentThread().getId();
        System.out.println("222 After test-method. Thread id is: " + id);
    }

    @AfterClass
    public void afterClass() {
        long id = Thread.currentThread().getId();
        System.out.println("222 After test-class. Thread id is: " + id);
    }
}
package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ParallelMethodTest {
    @BeforeClass
    public void beforeClass(){
        long id = Thread.currentThread().getId();
        System.out.println("111 Before class-method. Thread id is: " + id);

    }
     @BeforeMethod
        public void beforeMethod() {
            long id = Thread.currentThread().getId();
            System.out.println("111 Before test-method. Thread id is: " + id);
        }

        @Test
        public void testMethodsOne() {
            long id = Thread.currentThread().getId();
            System.out.println("111 Simple test-method One. Thread id is: " + id);
        }

        @AfterMethod
        public void afterMethod() {
            long id = Thread.currentThread().getId();
            System.out.println("111 After test-method. Thread id is: " + id);
        }

        @AfterClass
        public void AfterClass(){
            long id = Thread.currentThread().getId();
            System.out.println("111 After class-method. Thread id is: " + id);

        }
}
<suite name="Test-method Suite" parallel="classes" thread-count="2">
    <test name="Test-method test" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelMethodTest" />
            <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />

        </classes>
    </test>
</suite>

In the result , you could clearly to see that thread 10 is only working for the 111, and thread 11 working for the 222.

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

111 Before class-method. Thread id is: 10
111 Before test-method. Thread id is: 10
111 Simple test-method One. Thread id is: 10
111 After test-method. Thread id is: 10
111 After class-method. Thread id is: 10
222 Before test-class. Thread id is: 11
222 Before test-method. Thread id is: 11
222 Sample test-method One. Thread id is: 11
222 After test-method. Thread id is: 11
222 Before test-method. Thread id is: 11
222 Sample test-method Two. Thread id is: 11
222 After test-method. Thread id is: 11
222 After test-class. Thread id is: 11

===============================================
Test-method Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

ParallelMode.TESTS:
Same java code as above, with different testNG.xml:
1> 在同一个test标签中:

<suite name="Test-method Suite" parallel="tests" thread-count="2">
    <test name="Test-method test" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelMethodTest" />
            <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />

        </classes>
    </test>
    </suite>

得到如下结果:

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

111 Before class-method. Thread id is: 10
111 Before test-method. Thread id is: 10
111 Simple test-method One. Thread id is: 10
111 After test-method. Thread id is: 10
111 After class-method. Thread id is: 10
222 Before test-class. Thread id is: 10
222 Before test-method. Thread id is: 10
222 Sample test-method One. Thread id is: 10
222 After test-method. Thread id is: 10
222 Before test-method. Thread id is: 10
222 Sample test-method Two. Thread id is: 10
222 After test-method. Thread id is: 10
222 After test-class. Thread id is: 10

===============================================
Test-method Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

2> 在不同的Test标签中

<suite name="Test-method Suite" parallel="tests" thread-count="2">
    <test name="Test-method test" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelMethodTest" />

        </classes>
    </test>

    <test name="Test-method test2" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />
        </classes>
    </test>
</suite>

得到结果如下:多线程出现

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

222 Before test-class. Thread id is: 11
111 Before class-method. Thread id is: 10
111 Before test-method. Thread id is: 10
222 Before test-method. Thread id is: 11
111 Simple test-method One. Thread id is: 10
222 Sample test-method One. Thread id is: 11
222 After test-method. Thread id is: 11
111 After test-method. Thread id is: 10
111 After class-method. Thread id is: 10
222 Before test-method. Thread id is: 11
222 Sample test-method Two. Thread id is: 11
222 After test-method. Thread id is: 11
222 After test-class. Thread id is: 11

===============================================
Test-method Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
<think>我们正在处理用户关于TestNG库中特定类的问题。用户询问哪个jar包包含`org.testng.collections.Lists`类。根据引用内容,我们知道用户之前遇到了TestNG版本不兼容的问题(版本55.0与52.0),并尝试降级到6.9.9版本(引用[3])。此外,在引用[2]中出现了`org.testng.collections.Lists`的使用。关键点:-`org.testng.collections.Lists`是TestNG内部的一个工具类,用于集合操作(如创建ArrayList等)。-这个类属于TestNG的核心库,因此它应该包含在TestNG的jar包中。根据用户提供的引用[3],他们尝试使用TestNG6.9.9版本。在TestNG6.9.9中,这个类确实存在。实际上,在TestNG的多个版本中,这个类都存在于核心jar包中。因此,我们可以得出结论:包含`org.testng.collections.Lists`类的jar包就是TestNG的主jar包,通常命名为`testng-<version>.jar`。例如,在版本6.9.9中,jar包名为`testng-6.9.9.jar`。注意:用户之前可能使用了过新的版本(7.6.0)导致版本不兼容(因为7.6.0需要Java11,而用户环境可能是Java8)。所以建议用户使用6.9.9版本,这个版本兼容Java8(版本号52.0)。回答结构:1.直接回答问题:包含该类的jar包是TestNG的主jar包(testng-<version>.jar)。2.根据用户之前的上下文,建议使用6.9.9版本(因为用户已经尝试过并解决了版本问题)。3.如果用户需要,可以提供Maven依赖配置(引用[3]已经给出)。同时,生成相关问题以帮助用户深入理解。</think>### 包含 `org.testng.collections.Lists` 的 Jar 包 `org.testng.collections.Lists` 类是 **TestNG 框架** 的内部工具类,用于集合操作(如创建空列表、类型安全集合等)。它包含在 TestNG 的主 Jar 包中,具体如下: - **Jar 包名称**:`testng-<version>.jar` (例如:`testng-6.9.9.jar` 或 `testng-7.6.0.jar`) - **Maven 依赖**: ```xml <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.9</version> <!-- 或指定其他版本 --> </dependency> ``` #### 验证方法: 1. **代码中直接使用**: ```java import org.testng.collections.Lists; List<String> list = Lists.newArrayList(); // 创建空列表 ``` 2. **查看 Jar 包内容**: - 解压 `testng-*.jar` 文件,在路径 `org/testng/collections/` 下可找到 `Lists.class`。 #### 版本注意事项: - 若需兼容 **Java 8**(类文件版本 52.0),建议使用 **TestNG 6.9.9**(引用[3])[^3]。 更高版本(如 7.6.0)需 Java 11+(版本 55.0),否则会报错(引用[1])[^1]。 --- ### 相关问题 1. 如何解决 TestNG 的 `类文件具有错误的版本 55.0, 应为 52.0` 错误? 2. `org.testng.collections.Lists` 与 Guava 的 `Lists` 有何区别? 3. 如何在 Maven 项目中降级 TestNG 版本? 4. TestNG 中有哪些常用的集合工具类?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值