testng 取消并行
We can run TestNG tests in parallel. This feature is only available when we are using TestNG XML suite for executing our test cases.
我们可以并行运行TestNG测试。 仅当我们使用TestNG XML套件执行测试用例时,此功能才可用。
TestNG默认测试执行 (TestNG Default Tests Execution)
When we run TestNG class, methods are executed in order of the alphabetical appearance of their names. Let’s look at a simple TestNG test class.
当我们运行TestNG类时,方法将按照其名称的字母顺序出现。 让我们看一个简单的TestNG测试类。
package com.journaldev.parallel;
import java.time.LocalDateTime;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class NewTest {
@Test(dataProvider = "dp")
public void foo(Integer n) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Foo Executing Input = " + n);
}
@Test(dataProvider = "dp")
public void bar(Integer n) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Bar Executing Input = " + n);
}
@DataProvider
public Object[] dp() {
Object[] ints = new Object[100];
for (int i = 0; i < 100; i++)
ints[i] = i;
return ints;
}
@AfterTest
public void afterTest() {
System.out.println("After Test Time = " + LocalDateTime.now());
}
@BeforeTest
public void beforeTest() {
System.out.println("Before Test Time = " + LocalDateTime.now());
}
}
When we run above class, it will generate output like below.
当我们在类上运行时,它将生成如下所示的输出。
Before Test Time = 2018-06-01T15:03:21.406219
Bar Executing Input = 0
Bar Executing Input = 1
...
Bar Executing Input = 99
Foo Executing Input = 0
Foo Executing Input = 1
...
Foo Executing Input = 99
After Test Time = 2018-06-01T15:03:24.058081
Notice that bar() method is getting executed first, once all test executions are over then foo() methods are executed.
注意,bar()方法首先被执行,一旦所有测试执行都结束,然后将执行foo()方法。
We will get the similar result with below XML suite file.
下面的XML套件文件将得到相似的结果。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
<test name="TestNGXMLTest Test" verbose="2">
<classes>
<class name="com.journaldev.parallel.NewTest" />
</classes>
</test>
</suite>
并行的TestNG方法 (TestNG Methods in Parallel)
Since both the test methods are unrelated, we can run them in parallel and save time. We will have to add parallel
and thread-count
to our test
to achieve this.
由于两种测试方法都不相关,因此我们可以并行运行它们并节省时间。 我们将不得不增加parallel
和thread-count
来我们的test
,以实现这一目标。
Below is our updated XML suite file.
以下是我们更新的XML套件文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
<test name="TestNGXMLTest Test" verbose="2" parallel="methods" thread-count="2">
<classes>
<class name="com.journaldev.parallel.NewTest" />
</classes>
</test>
</suite>
When we run the test suite, we will get following output.
运行测试套件时,将获得以下输出。
Before Test Time = 2018-06-01T15:06:38.268674
Bar Executing Input = 0
Foo Executing Input = 0
...
Foo Executing Input = 99
After Test Time = 2018-06-01T15:06:39.572976
Notice that we almost cut the test execution time to half by running test methods in parallel.
注意,通过并行运行测试方法,我们几乎将测试执行时间减少了一半。
TestNG并行执行要点 (TestNG Parallel Execution Important Points)
- We can use
parallel
attribute for both test and suite elements. 我们可以对test和suite元素使用parallel
属性。 - Possible values for parallel attribute are: methods, tests, classes and instances. We have already seen how to execute methods in parallel. 并行属性的可能值为:方法,测试,类和实例。 我们已经看到了如何并行执行方法。
- In older TestNG versions,
true
andfalse
were also allowed for parallel values, they have been deprecated now. 在较旧的TestNG版本中,还为并行值允许使用true
和false
,现在已弃用它们。
并行运行TestNG运行类 (TestNG Running Classes in Parallel)
Let's say we want to run our test classes in parallel. We also want our methods inside the test classes to be running in parallel. We can do this by using parallel="classes"
at suite level and parallel="methods"
at test level.
假设我们要并行运行测试类。 我们还希望测试类中的方法可以并行运行。 我们可以通过在套件级别使用parallel="classes"
parallel="methods"
在测试级别使用parallel="methods"
来做到这一点。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite" parallel="classes">
<test name="TestNGXMLTest Test" verbose="2" parallel="methods"
thread-count="10">
<classes>
<class name="com.journaldev.parallel.NewTest" />
<class name="com.journaldev.utils.TestUtils" />
<class name="com.journaldev.main.Test5" />
</classes>
</test>
</suite>
TestNG Suite运行并行测试 (TestNG Suite Running Parallel Tests)
Let's look at a more complex example, where we are running TestNG suite tests in parallel. Again some of these tests are configured to execute classes/methods in parallel.
让我们看一个更复杂的示例,其中我们并行运行TestNG套件测试。 同样,这些测试中的一些配置为并行执行类/方法。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite" parallel="classes">
<test name="TestNGXMLTest Test 1" verbose="2" parallel="tests"
thread-count="10">
<classes>
<class name="com.journaldev.main.Test5" />
</classes>
</test>
<test name="TestNGXMLTest Test 2" verbose="2" parallel="classes"
thread-count="10">
<classes>
<class name="com.journaldev.utils.TestUtils" />
<class name="com.journaldev.main.Test5" />
</classes>
</test>
<test name="TestNGXMLTest Test 3" verbose="2" parallel="methods"
thread-count="10">
<classes>
<class name="com.journaldev.parallel.NewTest" />
</classes>
</test>
</suite>
That's all for running TestNG test suites in parallel, in different modes.
这就是在不同模式下并行运行TestNG测试套件的全部内容。
翻译自: https://www.journaldev.com/21341/testng-parallel-execution
testng 取消并行