zoukankan      html  css  js  c++  java
  • Junit Tests execution order

    From: http://www.java-allandsundry.com/2013/01/junit-test-method-ordering.html

    Junit until version 4.10 uses the order of test methods in a test class as returned by the reflection API as the order of test method execution –Class.getMethods(). To quote the Javadoc of getMethods() api:

    The elements in the array returned are not sorted and are not in any particular order.

    thus the order of test method execution in a test class is not predictable. This in a way is good as it encourages us as developers to write test methods which can stand on its own and to not depend on the order of test method execution.
     
     
     
    With version 4.11 of Junit the order of test method execution is not so unpredictable anymore, by default the order though not specified will be deterministic for every run. The order can further be enforced by adding a new annotation @FixMethodOrder to the test class with the following values:

    1. @FixMethodOrder(MethodSorters.DEFAULT) – deterministic order based on an internal comparator

    2. @FixMethodOrder(MethodSorters.NAME_ASCENDING) – ascending order of method names

    3. @FixMethodOrder(MethodSorters.JVM) – pre 4.11 way of depending on reflection based order

    Consider the following test case:

     1 public class ATest {
     2  @Test
     3  public void b_test_1() {
     4   System.out.println('b_test_1 called..');
     5  }
     6 
     7  @Test
     8  public void r_test_2() {
     9   System.out.println('r_test_2 called..');
    10  }
    11 
    12  @Test
    13  public void z_test_3() {
    14   System.out.println('z_test_3 called..');
    15  }
    16 
    17  @Test
    18  public void l_test_4() {
    19   System.out.println('l_test_4 called..');
    20  }
    21 }
    View Code

     Pre 4.11 running this test prints the following in my machine

    1 Running testorder.ATest
    2 r_test_2 called..
    3 z_test_3 called..
    4 b_test_1 called..
    5 l_test_4 called..
    View Code

    With NAME_ASCENDING:

    1 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    2 public class ATest
    View Code

    this is the output:

    1 Running testorder.ATest
    2 b_test_1 called..
    3 l_test_4 called..
    4 r_test_2 called..
    5 z_test_3 called..
    View Code

    There is still no way to explicitly specify other custom sorting methods which is good as the purpose of providing a predictable order is just that – to make it predictable, not to use it to add dependencies between test methods.

    Resource:

    JUnit Wiki – https://github.com/KentBeck/junit/wiki/Test-execution-order

  • 相关阅读:
    深入解读kubernetes网络基本原理
    Go!Go!Go!设计模式-组合设计模式
    Go!Go!Go!设计模式-创建型模式(简单工厂,工厂方法,抽象工厂)
    Linux内核之磁盘和分区
    Docker容器网络基础
    chart仓库之创建-入库-使用(helm,helm-push,chartmuseum)
    Go语言完整解析Go!Go!Go!(一)数据类型 之 Channel & Goroutine
    k8s爬坑集锦[网络问题]-服务无法访问
    数字证书的原理与应用&爬坑
    ingress的用法与原理
  • 原文地址:https://www.cnblogs.com/glf2046/p/6207009.html
Copyright © 2011-2022 走看看