【TestNG快板说四】TestNG测试依赖、随机运行、多个suite

TestNG测试依赖
两种依赖设置
  • 依赖方法method
  • 依赖组group
  1. 方法的依赖:testDemo1依赖testDemo2
    @Test(groups = {"smoke", "uat"}, dependsOnMethods = {"testDemo2"})
    public void testDemo1() throws InterruptedException{
        System.out.print("testDemo1\n");
    }

    @Test(groups = {"smoke", "func"})
    public void testDemo2() throws InterruptedException{
        System.out.print("testDemo2\n");
    }


testng.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
    <test name="Regression1">
        <parameter name="username" value="testname" /> <!-- 添加参数 -->
        <classes>
            <class name="com.nitb.demo.Demo1">
                <methods>
                    <include name="testDemo1" />
                    <include name="testDemo2" /> <!-- 这里看到testDemo2在testDemo1后面 -->
                </methods>
            </class>
        </classes>
    </test>
</suite>

从上面的xml看执行顺序应该是testDemo1–>testDemo2,但是由于设置了依赖关系,执行的顺序是testDemo2–>testDemo1,执行结果如下:

BeforeClass
testDemo2
testDemo1
AfterClass

===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================
  1. 组的依赖:testDemo1依赖group=start
    @Test(groups = {"smoke", "uat"}, dependsOnMethods = {"testDemo2"}, dependsOnGroups = {"start"})
    public void testDemo1() throws InterruptedException{
        System.out.print("testDemo1\n");
    }

    @Test(groups = {"smoke", "func"})
    public void testDemo2() throws InterruptedException{
        System.out.print("testDemo2\n");
    }

    ......
    
    @Test(groups = {"start"})
    public void  testDemo5() {
        System.out.print("testDemo5\n");
    }

testng.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
    <test name="Regression1">
        <parameter name="username" value="testname" /> <!-- 添加参数 -->
        <classes>
            <class name="com.nitb.demo.Demo1">
                <methods>
                    <include name="testDemo1" />
                    <include name="testDemo2" /> <!-- 这里看到testDemo2在testDemo1后面 -->
                    <include name="testDemo5" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

运行结果:

testDemo2
testDemo5
testDemo1

===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================

从运行结果可以看到,testDemo1运行所依赖的group=start的testDemo5先运行了。这里testDemo1既依赖testDemo2(方法依赖)又依赖testDemo5(组依赖),那么这两个的执行顺序靠什么决定?假设testDemo2和testDemo5没有关系,那么执行顺序就按照中的排列顺序来执行。

随机运行suite中的测试用例
  • 增加一个实现IMethodInterceptor的方法
public class TestOrderRandomizer  implements IMethodInterceptor {
    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> list, ITestContext iTestContext) {
        long seed = System.nanoTime();
        Collections.shuffle(list, new Random(seed));
        return list;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
    <listeners>
        <listener class-name="com.nitb.demo.TestOrderRandomizer" />
    </listeners>
    <test name="Regression1">
        <classes>
            <class name="com.nitb.demo.Demo2">
            </class>
        </classes>
    </test>
</suite>

执行的过程中,拦截所有TestNG加载的的方法,然后使用Collections.shuffle打乱列表中的顺序,返回给运行器执行。

多次运行结果:

第一次

BeforeClass
testDemo2
testDemo3
testDemo1
AfterClass

===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================

第二次

testDemo3
testDemo2
testDemo1
AfterClass

===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================

从上面的结果,可以看出,两次运行出来的结果顺序是不同的,随机的。

TestNG设置多个suite进行运行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
 
<suite name="ETS Suite">
    <suite-files>
        <suite-file path="test-suite1.xml" />
        <suite-file path="test-suite2.xml" />
    </suite-files>
</suite>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值