JUnit - 特定の順序でテストを実行する

JUnit –特定の順序でテストを実行する

JUnitでは、@FixMethodOrder(MethodSorters.NAME_ASCENDING)を使用して、辞書式順序でメソッド名でテストメソッドを実行できます。

P.S Tested with JUnit 4.12

ExecutionOrderTest.java

package com.example;

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

    }

}

出力、上記のテストメソッドは次の順序で実行されます。

test1
test2
testA
testB
testC

junit-execution-order

Note
JUnitは実行順序としてメソッド名のみを提供します。ユニットテストは分離して実行する必要があるため、JUnitチームはテスト実行順序をサポートする他の機能を開発する予定はないと思います。任意の実行順序。

テストの実行順序が本当に必要な場合は、TestNG Dependency Testを試してください