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

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

目前比较常见的有三种:

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

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

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

  1. import org.junit.FixMethodOrder;
  2. import org.junit.Test;
  3. import org.junit.runners.MethodSorters;
  4.  
  5. //@FixMethodOrder(MethodSorters.DEFAULT)
  6. //@FixMethodOrder(MethodSorters.NAME_ASCENDING)
  7. //@FixMethodOrder(MethodSorters.JVM)
  8. public class TestExecuteOrder {
  9.  
  10. @Test
  11. public void test03Third() {
  12. System.out.println("test03");
  13. }
  14.  
  15. @Test
  16. public void test01First() {
  17. System.out.println("test01");
  18. }
  19.  
  20. @Test
  21. public void test02Second() {
  22. System.out.println("test02");
  23. }
  24. }

执行结果如下

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

 

Test execution order的更多相关文章

  1. Execution Order of Event Functions

    In Unity scripting, there are a number of event functions that get executed in a predetermined order ...

  2. unity 脚本执行顺序设置 Script Execution Order Settings

     通过Edit->Project Settings->Script Execution Order打开MonoManager面板  或者选择任意脚本在Inspector视图中点击Execu ...

  3. Execution Order for the ApiController

    Execution Order for the ApiController Assuming the request goes into the ApiController scope, the op ...

  4. Unity3D Script Execution Order ——Question

    我 知道 Monobehaviour 上的 那些 event functions 是 在主线程 中 按 顺序调用的.这点从Manual/ExecutionOrder.html 上的 一张图就可以看出来 ...

  5. Execution Order In a Test Plan

    1.Config Element 2.Pre  Processors 3.Timer 4.Sampler 5.Post Processors 6.Assertions 7.Listener

  6. How to define Servlet filter order of execution using annotations

    If we define Servlet filters in web.xml, then the order of execution of the filters will be the same ...

  7. System and method for parallel execution of memory transactions using multiple memory models, including SSO, TSO, PSO and RMO

    A data processor supports the use of multiple memory models by computer programs. At a device extern ...

  8. Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译

    本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...

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

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

随机推荐

  1. PHP ServerPush <转>

    http://www.cnblogs.com/hnrainll/archive/2013/05/07/3064874.html

  2. iOS报错Expected selector for Objective-C method

    这个报错非常恶心:原因竟然是在导入头文件的地方多写了一个"+"号,可能问题在一个文件,报错在另一个文件

  3. javascript实现Map

    function Map() { this.elements = new Array(); // 获取MAP元素个数 this.size = function() { return this.elem ...

  4. sql查阅每一月的数据

    因为项目中需要做数据报表的功能,需要统计每个月的销售额.我找到下面的sql语句.后来经过自己的测试,发现第二句才是可以用的, //String sql="SELECT year(buydat ...

  5. (转)PHP文件没有结尾的?>有什么好处?

    1.PHP文件没有结尾的?>有什么好处?-- 防止输出一些不必要的空行或者空格2. 如果你是php和html混编的话结尾?> 还是有必要的,否则会报错. 如果没有?>文件末尾的空白行 ...

  6. Android LayoutInflater和findViewById 源码详解

    LayoutInflater大家很熟悉,简单点说就是布局文件XML解析器,setContentView函数也是调用了LayoutInflater 用法: View view = LayoutInfla ...

  7. Java基础--多线程的方方面面

    1,什么是线程?线程和进程的区别是什么? 2,什么是多线程?为什么设计多线程? 3,Java种多线程的实现方式是什么?有什么区别? 4,线程的状态控制有哪些方法? 5,线程安全.死锁和生产者--消费者 ...

  8. Maven Profile标签

    Maven Profiles标签可以针对不同的环境来使用不同的配置文件 在发布的时候可以用 mvn release -p product mvn release -p test mvn release ...

  9. Bit Map解析

    1. Bit Map算法简介 来自于<编程珠玑>.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空 ...

  10. 使用xshell出现乱码

    用xshell链接Linux出现乱码: 解决方法: 先查看Linux支持的字符类型是否为如下类型 如果是,则找到菜单中的文件选项,并在选项中找到属性: 单击属性选项,找到终端,将编码设置为UTF-8: ...