我有一个测试套件,其中我通过使用像这样的注释来使用并行性:测试名称=“所有常规测试” parallel =“ tests”线程计数=“ 30”
现在,假设我们在一个类中有多个@test批注。它也具有@beforeclass注释方法。因此,当单独的线程从同一个类中选择测试时,它将在两个线程中执行@beforeclass方法,还是将共享相同的数据。
或者我应该使用parallel =“ methods”,正确的方法是什么?
无法理解并行性的概念。
我明白你为什么感到困惑。
在测试标签中使用parallel="tests"
没有意义。parallel =“ tests”指示XML中的所有定义都在不同的线程中运行。但是您将其分配给测试级别,因此它将仅将该选项应用于该测试。
您在这里有两个选择。
将多线程选项置于套件级别,并带有“测试”并行化:<suite name = "ParallelTesting" parallel="tests" thread-count="2">
或将其置于测试级别,并带有“方法”选项:<test name = "Test Parallelism" parallel="method" thread-count="2">
所以:
关于第二个问题,不,它将只运行一次@BeforeClass。您可以很容易地对其进行测试:
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "ParallelTesting" parallel="tests" thread-count="2">
<test name = "Test Parallelism 1" >
<classes>
<class name = "com.openjaw.testregression.tretailapi.test.car.TestClass"/>
</classes>
</test>
<test name = "Test Parallelism 2" >
<classes>
<class name = "com.openjaw.testregression.tretailapi.test.car.TestClass2"/>
</classes>
</test>
</suite>
测试类别1:
public class TestClass {
@BeforeClass
public void beforeClass() {
System.out.println("Class 1 - Before Class with Thread Id:- "+ Thread.currentThread().getId());
}
@Test
public void testA() {
System.out.println("Class 1 - Test Case A with Thread Id:- " + Thread.currentThread().getId());
}
@Test
public void testB() {
System.out.println("Class 1 - Test Case B with Thread Id:- " + Thread.currentThread().getId());
}
}
测试类别2:
public class TestClass2 {
@BeforeClass
public void beforeClass() {
System.out.println("Class 2 - Before Class with Thread Id:- "+ Thread.currentThread().getId());
}
@Test
public void testA() {
System.out.println("Class 2 - Test Case A with Thread Id:- " + Thread.currentThread().getId());
}
@Test
public void testB() {
System.out.println("Class 2 - Test Case B with Thread Id:- " + Thread.currentThread().getId());
}
}
输出:
Class 1 - Before Class with Thread Id:- 12
Class 2 - Before Class with Thread Id:- 13
Class 2 - Test Case A with Thread Id:- 13
Class 1 - Test Case A with Thread Id:- 12
Class 2 - Test Case B with Thread Id:- 13
Class 1 - Test Case B with Thread Id:- 12
===============================================
ParallelTesting
Total tests run: 4, Failures: 0, Skips: 0
===============================================