preserve-order=true 无效的原因分析

       使用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

所有的@BeforeTest要先执行完毕才会去执行@Test

而如果你的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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值