zoukankan      html  css  js  c++  java
  • Test execution order

    刚开始的时候,JUnit并没有规定测试方法的调用执行顺序。方法通过映射的API返回的顺序进行调用。然 而,使用JVM顺序是不明智的,因为Java平台没有规定任何特定的顺序,事实上JDK7或多或少的返回的是随机顺序。大部分写的好的测试代码不会假定一 个顺序,在特定的平台上一个可预言的失败比一个随机的失败更好。

    从4.11版本开始,如果想要改变测试执行顺序,只要简单的加一个 @FixMethodOder 注释就可以。

    目前比较常见的有三种:

    @FixMethodOrder(MethodSorters.DEFAULT):默认顺序。由方法名的哈希码值决定执行顺序。由于哈希码的生成和OS有关,所以不用的OS可能会出现不一样的执行顺序。在某一操作系统上,多次执行的顺序不变。

    @FixMethodOrder(MethodSorters.JVM):由JVM来决定执行顺序。当然执行顺序随着每一次的测试可能会有所不用。

    @FixMethodOrder(MethodSorters.NAME_ASCENDING):由方法名的字典顺序来决定执行顺序。

    import org.junit.FixMethodOrder; 
    import org.junit.Test; 
    import org.junit.runners.MethodSorters; 
    
    //@FixMethodOrder(MethodSorters.DEFAULT) 
    //@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
    //@FixMethodOrder(MethodSorters.JVM) 
    public class TestExecuteOrder { 
    
    @Test 
    public void test03Third() { 
    System.out.println("test03"); 
    } 
    
    @Test 
    public void test01First() { 
    System.out.println("test01"); 
    } 
    
    @Test 
    public void test02Second() { 
    System.out.println("test02"); 
    } 
    } 

    执行结果如下

    1.什么都不加:

    test02
    test01
    test03

    2. @FixMethodOrder(MethodSorters.DEFAULT) :

    test02
    test01
    test03
    3. @FixMethodOrder(MethodSorters.NAME_ASCENDING):

    test01
    test02
    test03
    4. @FixMethodOrder(MethodSorters.JVM):

    test03
    test01
    test02

    或者

    test02
    test01
    test03

     
  • 相关阅读:
    Nhibernate 在一个项目中连接不同的数据库。记录
    sql2005 向2000导入数据。一些注意事项
    iis发布系统问题总结 .....关于handler的重写的应用
    ExecutorService 线程池
    Spring Boot 访问静态资源
    spring boot的核心注解
    日期处理工具类
    Json解析工具类
    (技能篇)Mysql在linux下的全量热备份
    Linux相关命令
  • 原文地址:https://www.cnblogs.com/miniren/p/4638514.html
Copyright © 2011-2022 走看看