TestNG官方网站:
http://testng.org/doc/documentation-main.html
5.10 - Parallelism and time-outs
You can instruct TestNG to run your tests in separate threads in various ways.
5.10.1 - Parallel suites
This is useful if you are running several suite files (e.g. "java org.testng.TestNG testng1.xml testng2.xml") and you want each of these suites to be run in a separate thread. You can use the following command line flag to specify the size of a thread pool:
java org.testng.TestNG |
The corresponding ant task name is suitethreadpoolsize.
5.10.2 - Parallel tests, classes and methods
The parallel attribute on the <suite> tag can take one of following values:
< suite name = "My suite" parallel = "methods" thread-count = "5" > |
< suite name = "My suite" parallel = "tests" thread-count = "5" > |
< suite name = "My suite" parallel = "classes" thread-count = "5" > |
< suite name = "My suite" parallel = "instances" thread-count = "5" > |
- parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
- parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
- parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
- parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
Additionally, the attribute thread-count allows you to specify how many threads should be allocated for this execution.
Note: the @Test attribute timeOut works in both parallel and non-parallel mode.
You can also specify that a @Test method should be invoked from different threads. You can use the attribute threadPoolSize to achieve this result:
@Test (threadPoolSize = 3 , invocationCount = 10 , timeOut = 10000 ) |
public void testServer() |
In this example, the function testServer will be invoked ten times from three different threads. Additionally, a time-out of ten seconds guarantees that none of the threads will block on this thread forever. <!------------------------------------- RERUNNING ------------------------------------>
如果生成数据的方法很复杂,需要重写编写代码实现。可以通过写数据集类,在测试方法中调用测试集数据。
package com.easyway.testng; import org.testng.annotations.DataProvider; /** * 如果生成数据的方法很复杂,需要重写编写代码实现。可以通过写数据集类,在测试方法中调用测试集数据 * * @author longgangbai * 2013-11-19 下午2:43:50 * */ public class DynmicDataProvider { @DataProvider(name = "create") public static Object[][] createData() { return new Object[][] { new Object[] { new Integer(42) } }; } }
package com.easyway.testng; import org.testng.annotations.Test; /** * 制定数据集的类的测试 * @author longgangbai * 2013-11-9 下午2:45:24 * */ @Test public class StaticDataTest { @Test(dataProvider = "create", dataProviderClass = DynmicDataProvider.class,threadPoolSize = 3, invocationCount = 10, timeOut = 10000) public void test(Integer n) { System.out.println(" n ="+n); } }
结果:
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1087250714\testng-customsuite.xml
n =42
PASSED: test(42)
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@14b84ad: 10 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@114629: 20 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@10a621a: 0 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4957c7: 0 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@c3dd7e: 0 ms