TestNG测试方法执行优先级

前言

TestNG的测试方法默认是按照方法名的字母顺序执行的,那么怎么自定义测试方法的执行顺序呢?

下面我们将介绍其中的一种方法。

正文

1、默认的测试方法执行顺序:默认按照测试方法的方法名字母顺序依次执行。

public class TestNGPriority {
    @BeforeClass
    public void beforeClass(){
        System.out.println("TestNGPriority------>>>beforeClass");
    }
    @AfterClass
    public void afterClass(){
        System.out.println("TestNGPriority------>>>afterClass");
    }
    @Test
    public void testMethod1(){
        System.out.println("TestNGPriority------>>>testMethod1");
    }
    @Test
    public void testMethod2(){
        System.out.println("TestNGPriority------>>>testMethod2");
    }
    @Test
    public void testMethod3(){
        System.out.println("TestNGPriority------>>>testMethod3");
    }
}

2、利用@Test注解的属性priority来控制测试方法的执行顺序。priority=0执行优先级最高。

public class TestNGPriority {
    @BeforeClass
    public void beforeClass(){
        System.out.println("TestNGPriority------>>>beforeClass");
    }
    @AfterClass
    public void afterClass(){
        System.out.println("TestNGPriority------>>>afterClass");
    }
    @Test(priority = 1)
    public void testMethod1(){
        System.out.println("TestNGPriority------>>>testMethod1");
    }
    @Test(priority = 2)
    public void testMethod2(){
        System.out.println("TestNGPriority------>>>testMethod2");
    }
    @Test(priority = 0)
    public void testMethod3(){
        System.out.println("TestNGPriority------>>>testMethod3");
    }
}

上述加上priority属性后的代码测试方法执行的顺序应该是testMethod3>testMethod1>testMethod2。

下面是执行结果,

三人行,必有我师焉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值