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);
}
};

DEFAULT Sorter

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());
}
};

NAME Sorter

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测试方法的执行顺序也不是确定的

[Junit] 测试方法执行顺序的更多相关文章

  1. SpringBoot单元测试中的测试方法执行顺序

    一.忽略方法@ignore 二.执行顺序@FixMethodOrder(MethodSorter.JVM) 我们在执行JUnit测试用例时,有时需要按照定义顺序执行单元测试方法,比如如在测试数据库相关 ...

  2. 一文搞懂Python Unittest测试方法执行顺序

    大家好~我是米洛! 欢迎关注我的公众号测试开发坑货,一起交流!点赞收藏关注,不迷路. Unittest unittest大家应该都不陌生.它作为一款博主在5-6年前最常用的单元测试框架,现在正被pyt ...

  3. Java之指定Junit测试方法的执行顺序举例

    问题描述: 大家都知道使用JUnit进行测试的时候,方法的执行顺序不是按照编写的先后顺序执行的,那么如何控制Junit的执行顺序呢? 解决方法: 在测试类上加 @FixMethodOrder 注解即可 ...

  4. [uiautomator篇] 设置@test的执行顺序

    http://jackyrong.iteye.com/blog/2025609 Brief Junit 4.11里增加了指定测试方法执行顺序的特性 测试类的执行顺序可通过对测试类添加注解 “@FixM ...

  5. Junit指定测试执行顺序

    原文链接: Test execution order原文日期: 2012年12月06日翻译日期: 2014年7月2日翻译人员: 铁锚说明: Junit4.11版本及以后才支持,建议升级到最新版本.按照 ...

  6. Springboot:单元测试@FixMethodOrder注解指定测试方法的执行顺序

    我们在写JUnit测试用例时,有时候需要按照定义顺序执行我们的单元测试方法,比如如在测试数据库相关的用例时候要按照测试插入.查询.删除的顺序测试.如果不按照这个顺序测试可能会出现问题,比如删除方法在前 ...

  7. JUnit test case 执行顺序

    转自:JUnit中按照顺序执行测试方式 很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前 ...

  8. JUnit中按照顺序执行测试方式

    很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前面,然后是修改或者查询,最后才是删除,以 ...

  9. junit里面Test Case的执行顺序

    这里讨论的是junit在ant运行的情况,其他build工具应该也适用,但具体没试验过. 首先运行junit时是按照脚本中文件夹的顺序执行,如下脚本会先执行test1目录下的测试,其实是test2目录 ...

随机推荐

  1. resin设置jvm参数

    http://www.quiee.com.cn/archives/592/ resin resin3.0 及前期版本内存设置, 如下: 启动时设置虚拟内存: unix> bin/httpd.sh ...

  2. To Use Genymotion

    Chinese Site:http://www.genymotion.cn/ Offical Site:http://www.genymotion.com/  Not available in Chi ...

  3. ParameterizedThreadStart,ThreadStart的使用,线程Thread传参数

    Thread threadWithParam = new Thread(new ParameterizedThreadStart(new ThreadTest().ShowMsg));//thread ...

  4. Inno Setup 打包的文件以管理员权限执行

    最近发现一个问题,就是Inno Setup打包的程序安装完毕后执行需求管理员权限的程序的时候会失败( inno createprocess   须要提升),解决问题的最简单办法就是打包的后的程序也以管 ...

  5. NoSQL摘录

    NoSQL泛指非关系型数据库,诸如Cassandra.MongoDB.Neo4J和Riak等.它们主张使用无模式(schemaless)的数据,可以运行在集群环境中. 开源的NoSQL数据库:Redi ...

  6. JavaScript正则表达式基础知识汇总

    一.创建正则对象: 1.构造函数RegExp创建正则对象 var pattern = new RegExp('s$'); //pattern匹配以s结尾的字符串 2.使用正则直接量 var patte ...

  7. c++ abs与fabs

    在stdlib.h中定义的abs只针对整数取决对值,如果要对浮点数取绝对值,应该用fabs(或fabsf). 而math.h中定义的abs是可以对浮点数取绝对值的. 所以如果包含了stdlib.h和m ...

  8. PHP的CURLOPT_POSTFIELDS参数使用数组和字符串的区别

    手册上解释: CURLOPT_POSTFIELDS  全部数据使用HTTP协议中的"POST"操作来发送.要发送文件,在文件名前面加上@前缀并使用完整路径.这个参数可以通过urle ...

  9. Python 常用内建模块(os, sys,random)

    一.os 模块 1,操作系统与环境变量 import osprint(os.name) #操作系统类型,如果是posix 说明系统是linux unix 或 mac os x :如果是nt 就是win ...

  10. 解决国内gem不能用的问题

    转自:http://www.haorooms.com/post/gem_not_use 最近在安装SASS的时候,用到gem命令,但是运行出行如下错误! C:\Users\len>gem ins ...