http://jackyrong.iteye.com/blog/2025609

Brief 
Junit 4.11里增加了指定测试方法执行顺序的特性

测试类的执行顺序可通过对测试类添加注解 “@FixMethodOrder(value)” 来指定,其中value 为执行顺序

三种执行顺序可供选择:默认(MethodSorters.DEFAULT),按方法名(MethodSorters.NAME_ASCENDING)和JVM(MethodSorters.JVM)

当没有指定任何顺序时,按默认来执行 
Sorters 
1. MethodSorters.DEFAULT 
默认顺序由方法名hashcode值来决定,如果hash值大小一致,则按名字的字典顺序确定 
由于hashcode的生成和操作系统相关(以native修饰),所以对于不同操作系统,可能会出现不一样的执行顺序,在某一操作系统上,多次执行的顺序不变 
实现代码:

复制代码 
/** 
     * DEFAULT sort order 
     */ 
    public static Comparator<Method> DEFAULT = new Comparator<Method>() { 
        public int compare(Method m1, Method m2) { 
            int i1 = m1.getName().hashCode(); 
            int i2 = m2.getName().hashCode(); 
            if (i1 != i2) { 
                return i1 < i2 ? -1 : 1; 
            } 
            return NAME_ASCENDING.compare(m1, m2); 
        } 
    }; 
复制代码 
2. MethodSorters.NAME_ASCENDING (推荐) 
按方法名称的进行排序,由于是按字符的字典顺序,所以以这种方式指定执行顺序会始终保持一致; 
不过这种方式需要对测试方法有一定的命名规则,如 测试方法均以testNNN开头(NNN表示测试方法序列号 001-999)

复制代码 
  /** 
     * Method name ascending lexicographic sort order, with {@link Method#toString()} as a tiebreaker 
     */ 
    public static Comparator<Method> NAME_ASCENDING = new Comparator<Method>() { 
        public int compare(Method m1, Method m2) { 
            final int comparison = m1.getName().compareTo(m2.getName()); 
            if (comparison != 0) { 
                return comparison; 
            } 
            return m1.toString().compareTo(m2.toString()); 
        } 
    }; 
复制代码 
3. MethodSorters.JVM 
按JVM返回的方法名的顺序执行,此种方式下测试方法的执行顺序是不可预测的,即每次运行的顺序可能都不一样(JDK7里尤其如此). 
Samples 
以下是对Win7 - JDK7 - Junit4.11 的执行结果

复制代码 
//@FixMethodOrder(MethodSorters.DEFAULT) 
//@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
@FixMethodOrder(MethodSorters.JVM) 
public class TestJunitOrder {

@Test    
    public void test003Third() {        
        
        System.out.println("test003Third"); 
    } 
    
    @Test    
    public void test001First() {        
        
        System.out.println("test001First"); 
    } 
    
    @Test    
    public void test002Second() {        
        
        System.out.println("test002Second"); 
    } 

复制代码 
1. DEFAULT 
结果始终为: 
test002Second 
test001First 
test003Third

2. NAME_ASCENDING 
结果始终为: 
test001First 
test002Second 
test003Third

3. JVM 
多数情况下 结果为: 
test002Second 
test001First 
test003Third 
偶尔出现: 
test001First 
test003Third 
test002Second 
Dig more .. 
实际上 Junit里是通过反射机制得到某个Junit里的所有测试方法,并生成一个方法的数组,然后依次执行数组里的这些测试方法; 
而当用annotation指定了执行顺序,Junit在得到测试方法的数组后,会根据指定的顺序对数组里的方法进行排序;

复制代码 
  public static Method[] getDeclaredMethods(Class<?> clazz) { 
        Comparator<Method> comparator = getSorter(clazz.getAnnotation(FixMethodOrder.class));//获取测试类指定的执行顺序

Method[] methods = clazz.getDeclaredMethods(); 
        if (comparator != null) { 
            Arrays.sort(methods, comparator);//根据指定顺序排序 
        }

return methods; 
    } 
复制代码 
三种执行顺序的定义如下:

复制代码 
/** 
     * Sorts the test methods by the method name, in lexicographic order, 
     * with {@link Method#toString()} used as a tiebreaker 
     */ 
    NAME_ASCENDING(MethodSorter.NAME_ASCENDING),

/** 
     * Leaves the test methods in the order returned by the JVM. 
     * Note that the order from the JVM may vary from run to run 
     */ 
    JVM(null),

/** 
     * Sorts the test methods in a deterministic, but not predictable, order 
     */ 
    DEFAULT(MethodSorter.DEFAULT); 
复制代码 
由上可以看出 当设置为MethodSorters.JVM时,其并没有提供一个Comparator的实现,所以执行方法的顺序实际上就是 clazz.getDeclaredMethods();得到的数组里方法的顺序,而由于java里对getDeclaredMethods返回的方法没有指定任何顺序,所以最终导致Junit测试方法的执行顺序也不是确定的

--------------------------------------------------------------------- 
例子:

  1. import org.junit.FixMethodOrder;
  2. import org.junit.Test;
  3. import org.junit.runners.MethodSorters;
  4. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  5. public class OrderedTestCasesExecution {
  6. @Test
  7. public void test001First() {
  8. System.out.println("Executing first test");
  9. }
  10. @Test
  11. public void test002Second() {
  12. System.out.println("Executing second test");
  13. }
  14. @Test
  15. public void test003Third() {
  16. System.out.println("Executing third test");
  17. }
  18. }

输出: 
  Executing first test 
Executing second test 
Executing third test

[uiautomator篇] 设置@test的执行顺序的更多相关文章

  1. testNG设置测试的执行顺序

    在java类中,设置Test的执行顺序可以使用priority,或者enabled等属性.但是在testng.xml中,需要设置它的 preserve-order="true" 另 ...

  2. 14、testng.xml 设置用例执行顺序

    目录如下: TestGroup.java 代码如下: package com.testng.cn; import org.testng.annotations.*; import static org ...

  3. Nginx 编译设置模块执行顺序

    Nginx编译时,配置"--add-module=xxx"可以加入模块,当我们需要按照指定顺序来设置过滤模块执行顺序时,先配置的"--add-module=xxx&quo ...

  4. Unity中脚本的执行顺序总结(@WhiteTaken)

    (Editor)以上是Unity官方文档中的截图,脚本在被挂载到物体上,会启用Editor的方法Reset. (Initialization)当执行脚本开始,初始化的过程中,依次执行的是Awake-& ...

  5. Unity3D中脚本的执行顺序和编译顺序

    http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与 ...

  6. 【转】Unity3D中脚本的执行顺序和编译顺序(vs工程引用关系)

    http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与 ...

  7. (转)Unity3D中脚本的执行顺序和编译顺序(vs工程引用关系)

    自:http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行 ...

  8. 【转】Unity3D中脚本的执行顺序和编译顺序

    支持原文,原文请戳: Unity3D中脚本的执行顺序和编译顺序 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与脚本有关的也就是编译和执行啦 ...

  9. Mysql 语句执行顺序

    1.这样一个问题,作为一个开发人员需要掌握数据库的哪些东西?  在开发中涉及到数据库,基本上只用到了sql语句,如何写sql以及对其进行优化就比较重要,那些mysql的厚本书籍针对的是DBA,我们只需 ...

随机推荐

  1. .aspx设置跨域

    在web.config添加节点 <system.webServer>下添加 <httpProtocol>      <customHeaders>        & ...

  2. CF1093D Beautiful Graph

    思路: 题目倒是没啥好说的,就是注意memset的效率问题.如果循环多次调用memset去初始化一个比较大的数组,那就会很费时间.就是因为这个被hack了.:( 实现: #include <bi ...

  3. 【进度总结】第一个web应用程序(未完成)

    web程序快速导航 使用Eclipse for Java EE Web Development,并配置Tomcat,这部分内容在众多教程中都描述的十分详细.我直接从代码部分开始记录流程: 这张图是We ...

  4. InitialContext与lookup

    Context initial = new InitialContext(); Object objref = initial.lookup("java:comp/env/ejb/Simpl ...

  5. ubuntu 16.0 利用ant编译 hadoop-eclipse-plugins2.6.0

    折腾了两天,抱着不放弃的精神,我终于编译出我自己所需的hadoop中在eclipse中的插件 在网上下载的可能因为版本不一致,在编译的时候出现各种各样的问题,包括你的eclipse版本和hadoop版 ...

  6. select a.no,a.name,b.subid,b.subname,c.score

    select a.no,a.name,b.subid,b.subname,c.score from a,b,c  where a.no = c.no and b.subid = c.subid ;

  7. Robot Framework(十三) 执行测试用例——创建输出

    3.5创建输出 执行测试时会创建几个输出文件,并且所有这些文件都与测试结果有某种关联.本节讨论创建的输出,如何配置它们的创建位置以及如何微调其内容. 3.5.1不同的输出文件 输出目录 输出文件 日志 ...

  8. Luogu P1782 旅行商的背包

    题目传送门 卡常背包果然名不虚传 算法主体就是两种背包分开跑,先跑多重背包,再跑奇货 不知道为什么,这题二进制拆分好像要比单调队列优化快一些 然后这题毒瘤的地方就出来了: 如果一件物品的体积\(\ti ...

  9. Bootstrap 网格系统(Grid System)实例1

    Bootstrap 网格系统(Grid System)实例:堆叠水平 <!DOCTYPE html><html><head><meta http-equiv= ...

  10. html5/css3响应式页面开发总结

    一,自适应和响应式的区别 自适应是一套模板适应所有终端,但每种设备上看到的版式是一样的,俗称宽度自适应. 响应式一套模板适应所有终端,但每种设备看到的版式可以是不一样的. 虽然响应式/自适应网页设计会 ...