既然是讨论执行顺序问题,那么用例肯定是批量执行的,批量执行的方法有mvn test、直接运行testng.xml文件,其中直接运行testng.xml文件的效果与pom文件中配置执行testng.xml效果是一样,所以本次只讨论mvn test 批量运行方式。

一、用例准备

1、 测试用例

编写一些测试用例,单纯为了测试,内容只进行输入,没有任何逻辑。

public class FirstTest {@Testpublic void testFirst(){System.err.println("first test");}
}

2、pom文件配置

要使用mvn test方式批量运行用例,需要在pom文件中配置以下内容

       <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19.1</version>           </plugin></plugins></build>

二、执行顺序梳理

1、mvn test 默认顺序

如果pom文件只是进行了上步骤的配置,那么执行mvn test,用例是多线程无序执行的,如果要按顺序执行要配置为单线程,在<plugin>标签内增加如下配置

                               <configuration><forkCount>1</forkCount>                  <reuseForks>false</reuseForks></configuration>

再次执行mvn test,我们会发现用例是单线程,按一定顺序执行的。但是是按照字母a-z的顺序执行的,其实这个排序对我们来说用处不大,我们写用例要求的是看名知意,不可能按照这个顺序来写。

2、priority 注解

testng提供了丰富的注解功能,priority标示用例执行的优先级,默认值为0,值越大优先级越低。比如:

public class FirstTest {@Test(priority = 2)public void testFirst(){System.err.println("first test");}
}
public class SecondTest {@Testpublic void testFirst(){System.err.println("second test");}
}
public class ThirdTest {@Test(priority = 1)public void testFirst() {System.err.println("third test");}
}执行结果:
second test
third test
first test

该注解对同一个类的多个方法也是适用的,比如:

public class FirstTest {@Test(priority=2)public void testFirst2(){System.err.println("first test2");}@Test(priority=3)public void testFirst3(){System.err.println("first test3");}@Test(priority=1)public void testFirst(){System.err.println("first test");}
}执行结果:
first test
first test2
first test3

那么我们就会想如果每个类中都有多个方法,且优先级是不同的,那么执行顺序又是怎么样的?比如:

public class FirstTest {@Test(priority=2)public void testFirst2(){System.err.println("first test2");}@Testpublic void testFirst3(){System.err.println("first test3");}@Test(priority=1)public void testFirst(){System.err.println("first test1");}
}
public class SecondTest {@Testpublic void testFirst(){System.err.println("second test");}
}public class ThirdTest {@Test(priority = 1)public void testFirst() {System.err.println("third test");}@Test(priority = 2)public void testFirst2() {System.err.println("third test 2");}
}运行结果:
first test3
second test
first test1
third test
first test2
third test 2

这种情况我们从结果可以看出并没有按照类的顺序执行,而是按照priority设置的等级高低,去执行的类。那么这个问题的原因是什么?有没有办法类按照顺序执行同时类中方法也按照顺序执行?这个问题我们放到后面讨论解决方法。

3、dependsOnGroups

public class FirstTest {@Test(dependsOnGroups={"second"})public void testFirst(){System.err.println("first test");}
}public class SecondTest {@Test(groups="second",dependsOnGroups={"third"})public void testFirst(){System.err.println("second test");}  public class ThirdTest {@Test(groups="third")public void testFirst() {System.err.println("third test");}
}执行结果:
third test
second test
first test

同样该方法适用于类的方法,比如:

public class FirstTest {@Test(dependsOnGroups={"first"},groups="second")public void testFirst2(){System.err.println("first test2");}@Test(groups="first")public void testFirst3(){System.err.println("first test3");}@Test(dependsOnGroups={"second"})public void testFirst(){System.err.println("first test1");}
}
执行结果:
first test3
first test2
first test1

4、dependsOnMethods

该方法只适用于同一个类的不同方法间,不能跨类适用。举例如:

public class Third10Test {@Test(dependsOnMethods = { "testFirst3" })public void testFirst1() {System.err.println("third10 test1");}@Testpublic void testFirst2() {System.err.println("third10 test2");}@Test(dependsOnMethods = { "testFirst2" })public void testFirst3() {System.err.println("third10 test3");}
}执行结果:
third10 test2
third10 test3
third10 test1

5、testng.xml

pom文件中在maven-surefire-plugin插件增加配置如下:

<configuration><suiteXmlFiles><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles>
</configuration>

testng.xml作为testng的灵魂,提供了很强大的配置功能,其它使用方式可以自己百度。本问只讨论类型的执行顺序问题。tesng.xml配置如下:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Suite1"  ><test name="Nopackage"><classes><class name="com.appiumforatk.SecondTest" /><class name="com.appiumforatk.FirstTest" /><class name="com.appiumforatk.ThirdTest" /></classes>   </test>
</suite>

执行mvn test 结果如下:

Second test

first test

third test

从结果可以看出,默认是按照顺序执行。其实在suite和test标签分别有preserve-order控制各自子标签的执行顺序,该值默认true。

如果我们把该值设置为false,可以看到用例不再按照配置的顺序执行了。如下:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Suite1"  ><test name="Nopackage" preserve-order="false"><classes ><class name="com.appiumforatk.SecondTest" /><class name="com.appiumforatk.FirstTest" /><class name="com.appiumforatk.ThirdTest" /></classes>   </test>
</suite>

执行mvn test命令,结果如下:

first test

Second test

third test

那么我们开始思考,如果testng.xml中设置了preserve-order= “true”,同时我们也设置了priority,那么执行顺序会怎么样?比如:我们只在SecondTest上加了priority=1,其它保持不变。

public class SecondTest {@Testpublic void testFirst(){System.err.println("Second test");}@Test(priority = 1)public void testFirst1(){System.err.println("Second test1");}
}
public class ThirdTest {@Testpublic void testFirst() {System.err.println("third test");}@Test(priority = 1)public void testFirst1() {System.err.println("third test1");}
}

mvn test执行结果如下:

Second test

first test

third test

Second test1

third test1

从结果可以看出同一priority优先级方法,按照配置的顺序执行。也即priority的优先级>preserve-order.那么我们回到2中的问题,原因是因为priority是在testng开始的时候全部加载进去,如果想实现按顺序执行完一个类的方法后,再执行另外一个类的方法,就要去改变方法的priority值,可以通过监听的方式实现。代码如下:

public class RePrioritizingListener implements IAnnotationTransformer {HashMap<Object, Integer> priorityMap = new HashMap<Object, Integer>();Integer class_priorityCounter = 10000;Integer max_testpriorityLength = 4;@Overridepublic void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod) {Class<?> declaringClass = testMethod.getDeclaringClass();Integer test_priority = annotation.getPriority();Integer current_ClassPriority = priorityMap.get(declaringClass);// 如果类没有设置过优先级,则进行设置。if (current_ClassPriority == null) {current_ClassPriority = class_priorityCounter++;priorityMap.put(declaringClass, current_ClassPriority);}//获取类中方法的优先级,如果小于四位数则左侧补充0以达到四位数String concatenatedPriority = test_priority.toString();while (concatenatedPriority.length() < max_testpriorityLength) {concatenatedPriority = "0" + concatenatedPriority;}//把类的优先级和方法的优先级合并concatenatedPriority = current_ClassPriority.toString()+ concatenatedPriority;//重新设置方法的优先级annotation.setPriority(Integer.parseInt(concatenatedPriority));}
}

testng.xml配置如下:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Suite1" ><test name="Nopackage" preserve-order="true"><classes ><class name="com.appiumforatk.SecondTest" /><class name="com.appiumforatk.FirstTest" /><class name="com.appiumforatk.ThirdTest" /></classes>   </test><listeners>    //配置监听<listener class-name="com.appiumforatk.RePrioritizingListener"/></listeners>
</suite>

mvn test执行命令结果如下:

Second test

Second test1

first test

third test

third test1

这个时候我们看到按照配置的顺序执行class并且class中的方法也是按照priority优先级执行的。

参考文章:

https://www.cnblogs.com/leohou/p/11694807.html

TestNG测试框架之测试用例的执行顺序分析相关推荐

  1. java testng 优化_java+testNG测试框架搭建----jenkins自动化执行

    新建项目,在pom.xml里添加要执行的测试类:suites/test.xml [本地生成的测试报告暂时屏蔽掉] src/main/java src/main/java ${project.artif ...

  2. (一)TestNG测试框架(含Demo源码)

    目录 一.TestNG是什么 二.关于TestNG使用 三.关于TestNG流程控制 四.结合场景讲解TestNG注解 五.TestNG框架(Demo源码) 六.补充知识点 一.TestNG是什么 T ...

  3. python装饰器执行顺序_python unittest单元测试框架-3用例执行顺序、多级目录、装饰器、fixtures...

    1.用例执行顺序 unittest默认会按照ascii码的顺序,依次执行.类名--方法名排序,使用discover也是默认排序.如果不想使用默认排序,就使用testsuite测试集的方式. impor ...

  4. maven TestNg 测试框架 not found

    在用TestNG测试框架进行单元测试时,在我的pom.xml中写的依赖: <dependency><groupId>org.testng</groupId>< ...

  5. mysql 执行顺序 SQL语句执行顺序分析

    最近在做一个数据库的大作业,算是复习了下MySql里面比较复杂的一些语句的用法,如Left Join之类的.在这里就不对具体语法进行记录了,希望能在以后经常用到,而不是隔好长时间用一次.在这里就记录下 ...

  6. testng执行参数_初识TestNG测试框架

    testkuaibao|软件测试自学公众号 公众号文章的推送机制改变.又由于我们公众号是不定时更新的,所以会导致很多小伙伴不能及时的收到我们的文章.大家可以把我们的公众号设置为星标,或者看完文章点个在 ...

  7. (二)TestNG测试框架之注解及属性概览

    前言 TestNG提供了诸多注解,允许开发/测试人员灵活地组织强大的测试用例. 注解概览 注解/属性 描述 @BeforeSuite @AfterSuite @BeforeTest @AfterTes ...

  8. TestNG测试框架介绍整理

    TestNG学习 什么是TestNG 添加pom maven依赖 Idea创建module 注解之@BeforeMethod和@AfterMethod 注解之@BeforeClass和@AfterCl ...

  9. python测试框架untest怎么循环执行_Python自动化测试-Unittest单元测试框架详解

    python中unittest模块是用来做单元测试的. unittest是一个python版本的junit,junit是java中的单元测试框架,对java的单元测试,有一句话很贴切:Keep the ...

最新文章

  1. 24、嵌合体序列Chimeras
  2. 开源高性能异步网关:Soul
  3. VS2012及VS系列怎样屏蔽CMD窗口~
  4. [原创]教你如何最快写出酷炫的dialog对话框
  5. 博客会被搬去csdn
  6. 总结PHP如何获取当前主机、域名、网址、路径、端口和参数等
  7. 删除JavaScript对象中的元素
  8. iBatis学习第一天
  9. SAP系统和微信集成的系列教程之一:微信开发环境的搭建
  10. java实现自动登录,并获取数据
  11. vue报错Invalid Host header
  12. 在.Net中json应用测试整理
  13. lesson 2.4 - Converting MEL Commands to Python
  14. MySQL数据表格导入导出
  15. 单片机中断实验 EX0
  16. 计算机程序员英文作文,程序员英文自我介绍3篇
  17. openwrt设置DNS
  18. 前端速成:双月Java之旅(week5)_day4
  19. 抖音创作者身份类型和视频类型
  20. SVG标准解读-几何图形-图案填充-核心要点

热门文章

  1. ps怎么更改背景图层大小_PS软件零基础抠图教程,教你PS滤镜抠图技巧和方法
  2. MySQL:SELECT COUNT 小结
  3. Facebook 分享 MySQL 5.6 到 8.0 的迁移经验
  4. 用低代码平台开发比用IDEA还牛逼吗?
  5. 皮一皮:我也想做这样的房东,善解人意、为他人着想...
  6. Redis+Nginx+设计模式+Spring全家桶+Dubbo+阿里P7技术精选文档
  7. 每日一皮:当我看到Bug背后的一切...我退缩了...
  8. 接地气的数据分析入门与进阶
  9. linux系统无线驱动在哪下载,在linux上怎么安装无线网卡驱动?
  10. cannot find -lcudart