Junitテストケースは順番に実行する


Junitテストケースの実行順序による実行には3つの方法があり,デフォルトではtestcaseの実行時に関数作成順序による実行ではない.

1、デジタルコード法


テスト・インスタンスのセットを順番に実行するように定義するには、testcase(テストケース)関数名の接頭辞が同じで、末尾が数値である場合、テストケースのセットは数値順に実行されます.例えば、testFirst 1、testFirst 2、testFirst 3の3つのテストケースがある場合、3つのテストケースの実行順序は、
testFirst1>testFirst2>testFirst3
import java.util.logging.Logger;

import org.junit.BeforeClass;
import org.junit.Test;

public class Test4 {
    private static final Logger LOG = Logger.getLogger(Test4.class.getName());

	@BeforeClass
	static public void beforeTest(){
		System.out.println("Method beforeTest invoked!");
		LOG.info("Method beforeTest invoked!");
	}
	
	@Test
	public void testa1(){
		System.out.println("Method test1 invoked!");
		LOG.info("Method test1 invoked!");
	}
	
	@Test
	public void testd2(){
		System.out.println("Method ctest2 invoked!");
		LOG.info("Method ctest2 invoked!");
	}
	
	@Test
	public void testc3(){
		System.out.println("Method dtest3 invoked!");
		LOG.info("Method dtest3 invoked!");
	}
	
	@Test
	public void testb4(){
		System.out.println("Method btest4 invoked!");
		LOG.info("Method btest4 invoked!");
	}
	
	@Test
	public void testFirst3(){
		System.out.println("Method First3 invoked!");
		LOG.info("Method First3 invoked!");
	}
	
	@Test
	public void testFirsa1(){
		System.out.println("Method First1 invoked!");
		LOG.info("Method First1 invoked!");
	}	
	
	@Test
	public void testFirsb1(){
		System.out.println("Method Firstb1 invoked!");
		LOG.info("Method Firstb1 invoked!");
	}
} 

テスト結果:
Method beforeTest invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 beforeTest
 : Method beforeTest invoked!
Method test1 invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 testa1
 : Method test1 invoked!
Method btest4 invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 testb4
 : Method btest4 invoked!
Method dtest3 invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 testc3
 : Method dtest3 invoked!
Method ctest2 invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 testd2
 : Method ctest2 invoked!
Method First1 invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 testFirsa1
 : Method First1 invoked!
Method Firstb1 invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 testFirsb1
 : Method Firstb1 invoked!
Method First3 invoked!
  27, 2017 10:07:02   com.hf.junit.test.Test4 testFirst3
 : Method First3 invoked!

テストケース関数名の末尾が数値でない場合は、無秩序に実行されます.
import java.util.logging.Logger;

import org.junit.Test;

public class Test2 {

    private static final Logger LOG = Logger.getLogger(Test2.class.getName());
    
    @Test
    public void testFirst() throws Exception {
    	System.out.println("------1--------");
        LOG.info("------1--------");
    }

    @Test
    public void testSecond() throws Exception {
    	System.out.println("------2--------");
        LOG.info("------2--------");

    }

    @Test
    public void testThird() throws Exception {
    	System.out.println("------3--------");
        LOG.info("------3--------");
    }
    
    @Test
    public void testFour() throws Exception {
    	System.out.println("------4--------");
        LOG.info("------4--------");
    }

}

テスト結果:
------1--------
  27, 2017 10:08:16   com.hf.junit.test.Test2 testFirst
 : ------1--------
------3--------
  27, 2017 10:08:16   com.hf.junit.test.Test2 testThird
 : ------3--------
------4--------
  27, 2017 10:08:16   com.hf.junit.test.Test2 testFour
 : ------4--------
------2--------
  27, 2017 10:08:16   com.hf.junit.test.Test2 testSecond
 : ------2--------

2、明示的な指定はアルファベット順に実行する


@FixMethodOrder(MethodSorters.NAME_ASCENDING)注記宣言によりアルファベット順に実行します.
import java.util.logging.Logger;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Test2FixOrder {

    private static final Logger LOG = Logger.getLogger(Test2FixOrder.class.getName());
    
    @Test
    public void testFirst() throws Exception {
    	System.out.println("------1--------");
        LOG.info("------1--------");
    }

    @Test
    public void testSecond() throws Exception {
    	System.out.println("------2--------");
        LOG.info("------2--------");

    }

    @Test
    public void testThird() throws Exception {
    	System.out.println("------3--------");
        LOG.info("------3--------");
    }
    
    @Test
    public void testFour() throws Exception {
    	System.out.println("------4--------");
        LOG.info("------4--------");
    }

}

テスト結果:
------1--------
  27, 2017 10:09:22   com.hf.junit.test.Test2FixOrder testFirst
 : ------1--------
------4--------
  27, 2017 10:09:22   com.hf.junit.test.Test2FixOrder testFour
 : ------4--------
------2--------
  27, 2017 10:09:22   com.hf.junit.test.Test2FixOrder testSecond
 : ------2--------
------3--------
  27, 2017 10:09:22   com.hf.junit.test.Test2FixOrder testThird
 : ------3--------
説明:@FixMethodOrder実行順序宣言には、上記のようにアルファベット順に「@FixMethodOrder(MethodSorters.NAME_ASCENDING)」を実行するほか、2種類の@FixMethodOrder(MethodSorters.DEFAULT),@FixMethodOrder(MethodSorters.JVM)があり、ここでJVMは関数の出現順に逆方向に実行され(テスト結果のように)、DEFAULT方式では無秩序に実行される.

3、テストキット法


テストキットを使用してテスト順序を整理し、テスト用例の実行プロセスをテストキット(TestSuit)に追加する順序で実行すると、TestSuitをTestCaseの配列と見なし、配列インデックス0の位置から実行することができます.
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class TestSuit1 extends TestCase {

	public TestSuit1(String name) {
		super(name);
	}

	public void test1() throws Exception {
		System.out.println("------ Test case 1 -------");
	}

	public void ctest2() {
		System.out.println("------ Test case 2 -------");
	}

	public void dtest3() {
		System.out.println("------ Test case 3 -------");
	}

	public void btest4() {
		System.out.println("------ Test case 4 -------");
	}

	public static Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new TestSuit1("test1"));
		suite.addTest(new TestSuit1("dtest3"));
		suite.addTest(new TestSuit1("ctest2"));		
		suite.addTest(new TestSuit1("btest4"));
		return suite;
	}
}

テスト結果:
------ Test case 1 -------
------ Test case 3 -------
------ Test case 2 -------
------ Test case 4 -------