JUnit Run test in a particular order

摘要: In JUnit, you can use @FixMethodOrder(MethodSorters.NAME_ASCENDING) to run the test methods by method name, in lexicographic order.

In JUnit, you can use @FixMethodOrder(MethodSorters.NAME_ASCENDING) to run the test methods by method name, in lexicographic order.

P.S Tested with JUnit 4.12

ExecutionOrderTest.java
package com.mkyong;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
//Sorts by method name
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ExecutionOrderTest {
    @Test
    public void testB() {
        assertThat(1 + 1, is(2));
    @Test
    public void test1() {
        assertThat(1 + 1, is(2));
    @Test
    public void testA() {
        assertThat(1 + 1, is(2));
    @Test
    public void test2() {
        assertThat(1 + 1, is(2));
    @Test
    public void testC() {
        assertThat(1 + 1, is(2));

Output, the above test methods will run in the following order :

test1
test2
testA
testB
testC
Note
JUnit only provides the method name as the execution order, and I think the JUnit team has no plan to develop other features to support the test execution order, because, unit test should run isolated and in ANY execution order.

If you really need the test execution order, try TestNG Dependency Test

References

  1. JUnit MethodSorters JavaDoc
  2. JUnit Test execution order

上一篇: Java Custom Exception Examples
下一篇: Intellij + Infinitest Continuous Testing
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号