Java+Selenium3.0基础篇(03):如何控制测试方法的执行顺序(Junit/TestNG)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36396763/article/details/89252752

Junit和TestNG中分别如何控制测试方法的执行顺序

首先如果我们不进行设置的话,各个测试方法会默认按照方法名的首字母顺序执行。
如果要控制它们的执行顺序,如下:

Junit:

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)//按字母顺序执行
//@FixMethodOrder(MethodSorters.DEFAULT)//随机顺序执行,可自行尝试

    public class TestOrder{
        @Test
        public void testFirst() throws Exception {
            System.out.println("1");
        }
        @Test
        public void testSecond() throws Exception {
            System.out.println("2");
        }
        @Test
        public void testThird() throws Exception {
            System.out.println("3");
        }
    }

Output:
1
2
3

TestNG(属于Junit的升级优化版)使用priority来控制顺序,0>1>2>3……,0为最高级:

//技术博客引用:https://www.cnblogs.com/janson071/p/10008764.html

import org.testng.annotations.Test;
public class PriorityTest {
    @Test(priority = 3)
    public void priorityTest1(){
        System.out.println("priorityTest1 运行");
    }
    @Test(priority = 2)
    public void priorityTest2(){
        System.out.println("priorityTest2 运行");
    }
    @Test(priority = 1)
    public void priorityTest3(){
        System.out.println("priorityTest3 运行");
    }
    @Test(priority = 0)
    public void priorityTest4(){
        System.out.println("priorityTest4 运行");
    }
}

另外比较建议使用博主这样的命名方式,可能会比较清晰:
类的命名:
在这里插入图片描述
类内方法的命名:
在这里插入图片描述
我是直接按顺序命名了方法,这样在执行的时候会自然按命名的顺序走。会很便捷。

PS:本文所讲述的是在类内部控制用例执行顺序的方法,如果你要找的是控制各个测试类的执行顺序的方法,详见我的这篇博客啦:
https://blog.csdn.net/qq_36396763/article/details/89376053


软件测试工程师一只,也在不断的学习阶段,平时的小经验不定期分享。
博主经验有限,若有不足,欢迎交流,共同改进~
愿与同在CSDN的你共同进步。
有意可加QQ1255187803交流学习。

猜你喜欢

转载自blog.csdn.net/qq_36396763/article/details/89252752