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..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:本文讲述的是在类内部控制用例执行顺序的方法,如果你要找的是控制各个测试类的执行顺序的方法,详见我的这篇博客啦:
自动化测试中如何控制各个测试类的执行顺序
软件测试工程师一只,也在不断的学习阶段,平时的小经验不定期分享。
博主经验有限,若有不足,欢迎交流,共同改进~
愿与同在的你共同进步。
有意可加QQ1255187803交流学习。