使用testNG suite测试的时候,如果一个<test>包含多个class,你又想指定执行的顺序,通常的做法是加入这样的属性<test name="testng_recharge3" preserve-order="true"> ,但是实际使用中要注意,如果你的class中包含有BeforeTest方法,就算你指定了preserve-order="true",BeforeTest方法一定是先执行的,等待所有的BeforeTest方法执行完毕才会执行@Test方法,至于其他的BeforeClass属性,我没试过,但是一样存在同样的问题,那么,如果你想每个Test和他配套的Before一起执行的话,就要在testNg.xml中设置成多个<test>标签,具体的代码和执行结果,如下所示:
test001.java
public class test001 {
@BeforeTest
public void initmodule() {
System.out.println("进入到test001!");
}
@Test
public void testng() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
System.out.println("this is test001");
}
}
test002.java
public class test002 {
@BeforeTest
public void initmodule() {
System.out.println("进入到test002!");
}
@Test
public void testng() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
System.out.println("this is test002");
}
}
test003.java
public class test003 {
@BeforeTest
public void initmodule() {
System.out.println("进入到test003!");
}
@Test
public void testng() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
System.out.println("this is test003");
}
}
如果你配置成下面这个方式,注意,指定了preserve-order="true"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="fulltests">
<test name="testng_recharge" preserve-order="true">
<classes>
<class name="com.test.shangfu.testsuite.test001" />
<class name="com.test.shangfu.testsuite.test002" />
<class name="com.test.shangfu.testsuite.test003" />
</classes>
</test>
</suite>
那么执行结果是这样的:
进入到test001!
进入到test002!
进入到test003
this is test001
this is test002
this is test003
而如果你的testng.xml配置成下面这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
<test name="testng_recharge1">
<classes>
<class name="com.test.shangfu.testsuite.test001" />
</classes>
</test>
<test name="testng_recharge2">
<classes>
<class name="com.test.shangfu.testsuite.test002" />
</classes>
</test>
<test name="testng_recharge3" >
<classes>
<class name="com.test.shangfu.testsuite.test003" />
</classes>
</test>
</suite>
那么执行结果是:
进入到test001!
this is test001
进入到test002!
this is test002
进入到test003
this is test003