二、通過preserve-order="true"
在xml添加<suite name="Suite" preserve-order="true">,方法將按照XML中配置的先后順序由上按下執行(代碼無需額外控制)
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" preserve-order="true">
<test name="Test">
<classes>
<class name="test.testng.TestOrder"/>
<methods>
<include name="test2" />
<include name="test3" />
<include name="test1" />
</methods>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
執行順序,打印結果:
this is test2
this is test3
this is test1
三、并發執行,Parallel
1、如果沒有并發執行用例的需要,建議把Parallel=false,如果等于Parallel=true,則會并發執行用例,除非你設置了Dependencies,否則執行的順序將不受控制
如:<suite name="Suite" parallel="true">
2、添加Parallel的時候也可以設置線程數,如:
<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.