在 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=&#