如果我们将 parallel="tests" 放在同一线程中,@beforeclass 方法将在同一线程中执行.在套件 xml 中 [英] @beforeclass methods get exexcuted in same thread if we put parallel="tests" in the suite xml
问题描述
我有一个测试套件,我通过使用以下注释来使用并行性:测试名称="所有常规测试" parallel="tests" thread-count="30"
I have a test suite where i am using parallelism by using annotation like : test name="All Regular Tests" parallel="tests" thread-count="30"
现在,假设我们在一个类中有多个 @test 注释.它也有@beforeclass 注释方法.因此,当单独的线程从同一个类中选择测试时,它会在两个线程中执行 @beforeclass 方法还是共享相同的数据.
Now, let's say we have multiple @test annotation in one class. and it has @beforeclass annotation method as well. So when separate thread will pick the tests from same class, will it execute the @beforeclass methods in both threads or will it share the same data.
或者我应该使用parallel="methods",正确的方法是什么?
Or shall i use parallel="methods", what is the correct way ?
无法理解并行的概念.
推荐答案
我明白你为什么感到困惑.
I can see why you are confused.
在测试标签中使用 parallel="tests"
是没有意义的.parallel="tests" 规定 XML 中定义的所有内容都在不同的线程中运行.但是您将其分配给测试级别,因此它只会将该选项应用于该测试.
It makes no sense to use parallel="tests"
in the test tag.
parallel="tests" is dictating that all the defined in the XML are run in different threads. But you are assigning that to a test level, so it will only apply that option for that test.
这里有两种选择.
将多线程选项置于套件级别,并使用测试"并行化:
To put the multithreading option at a suite level, with the "tests" parallelization:
<suite name = "ParallelTesting" parallel="tests" thread-count="2">
或者把它放在测试级别,使用方法"选项:
Or to put it on the test level, with "method" option:
<test name = "Test Parallelism" parallel="method" thread-count="2">
所以:
- 测试":针对 XML 文件中的每个.
- 方法":对于放置属性的每个@Test方法(套件中的所有@Tests,类中的所有@Tests等)
- "tests": for each in the XML file.
- "method": for each @Test method in wherever you put the property (all the @Tests in the suite, all the @Tests in a class, etc.)
关于你的第二个问题,不,它只会运行@BeforeClass 一次.您可以非常轻松地对其进行测试:
Regarding yout second question, no, it will run the @BeforeClass only once. You can put it to the test very easily:
XML 文件:套件级别的 parallel=tests:
<?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.test.testing.test.TestClass"/>
</classes>
</test>
<test name = "Test Parallelism 2" >
<classes>
<class name = "com.test.testing.test.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
===============================================
<小时>
并行化的另外两个变体是(使用上面相同的类):
The two other variations of parallelization are (using the same classes above):
XML 文件:parallel=method 在套件级别:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "ParallelTesting2" parallel="methods" thread-count="2">
<test name = "Test Parallelism 1" >
<classes>
<class name = "com.test.testing.test.TestClass"/>
</classes>
</test>
<test name = "Test Parallelism 2" >
<classes>
<class name = "com.test.testing.test.TestClass2"/>
</classes>
</test>
</suite>
输出:
Class 1 - Before Class with Thread Id:- 12
Class 1 - Test Case B with Thread Id:- 13
Class 1 - Test Case A with Thread Id:- 12
Class 2 - Before Class with Thread Id:- 14
Class 2 - Test Case A with Thread Id:- 14
Class 2 - Test Case B with Thread Id:- 15
<小时>
XML 文件:parallel=method 在测试级别:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "ParallelTesting2" >
<test name = "Test Parallelism 1" parallel="methods" thread-count="2">
<classes>
<class name = "com.test.testing.test.TestClass"/>
</classes>
</test>
<test name = "Test Parallelism 2" parallel="methods" thread-count="2">
<classes>
<class name = "com.test.testing.test.TestClass2"/>
</classes>
</test>
</suite>
输出:
Class 1 - Before Class with Thread Id:- 12
Class 1 - Test Case A with Thread Id:- 12
Class 1 - Test Case B with Thread Id:- 13
Class 2 - Before Class with Thread Id:- 14
Class 2 - Test Case B with Thread Id:- 15
Class 2 - Test Case A with Thread Id:- 14
<小时>
最后,我提到的这样做没有意义:
And finally, what I mention it wouldn't make sense to do:
XML 文件:parallel=tests on test level:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "ParallelTesting2" >
<test name = "Test Parallelism 1" parallel="tests" thread-count="2">
<classes>
<class name = "com.test.testing.test.TestClass"/>
</classes>
</test>
<test name = "Test Parallelism 2" parallel="tests" thread-count="2">
<classes>
<class name = "com.test.testing.test.TestClass2"/>
</classes>
</test>
</suite>
输出:
Class 1 - Before Class with Thread Id:- 1
Class 1 - Test Case A with Thread Id:- 1
Class 1 - Test Case B with Thread Id:- 1
Class 2 - Before Class with Thread Id:- 1
Class 2 - Test Case A with Thread Id:- 1
Class 2 - Test Case B with Thread Id:- 1
正如您在最后一个示例中所见,所有内容都在同一线程上运行,因为您要求每个测试(方法组)并行运行测试.
As you can see in that last example, everything runs on the same thread because you are asking to each test (group of methods) to run tests in parallel.
这篇关于如果我们将 parallel="tests" 放在同一线程中,@beforeclass 方法将在同一线程中执行.在套件 xml 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!