본문 바로가기

프로그래밍

JUnit 테스트 순서 지정하기

반응형

JUnit4에서는 아래와 같이 작성합니다.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JUnitTest {
    @Test
    public void TestA() throws Exception {
    }

    @Test
    public void TestB() throws Exception {
    }
}

https://junit.org/junit4/javadoc/latest/org/junit/runners/MethodSorters.html

 

MethodSorters (JUnit API)

DEFAULT           Sorts the test methods in a deterministic, but not predictable, order

junit.org

엇. 그런데. 적용이 안되네. 싶어서 확인해보니 JUnit5를 쓰고 있군요. 이런.

JUnit 5에서는 해당 API가 없어졌습니다. 그리고 5.3 버전까지는 테스트 순서 지정하는 방법이 없구요 (찾지 못했습니다 ㅠㅠ). 5.4부터는 새로운 API를 지원합니다. 5.4는 현재 RC1입니다.

https://junit.org/junit5/docs/5.4.0-RC1/release-notes/

 

JUnit 5 Release Notes

Date of Release: November 25, 2018 Scope: Bug fixes since 5.3.1 For a complete list of all closed issues and pull requests for this release, consult the 5.3.2 milestone page in the JUnit repository on GitHub. JUnit Platform Bug Fixes When configured with -

junit.org

@TestMethodOrder(OrderAnnotation.class)
public class JUnitTest {
    @Test
    @Order(1)
    public void TestA() throws Exception {
    }

    @Test
    @Order(2)
    public void TestB() throws Exception {
    }
}

https://junit.org/junit5/docs/5.4.0-RC1/api/org/junit/jupiter/api/TestMethodOrder.html

 

TestMethodOrder (JUnit 5.4.0-RC1 API)

@TestMethodOrder is a type-level annotation that is used to configure a MethodOrderer for the test methods of the annotated test class or test interface. In this context, the term "test method" refers to any method annotated with @Test, @RepeatedTest, @Par

junit.org

https://junit.org/junit5/docs/5.4.0-RC1/user-guide/index.html#writing-tests-test-execution-order

 

JUnit 5 User Guide

Although the JUnit Jupiter programming model and extension model will not support JUnit 4 features such as Rules and Runners natively, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and cus

junit.org

Photo by Micheile Henderson on Unsplash

OrderAnnotation을 사용하면 좀 더 깔끔하게 테스트 순서를 지정할 수 있어서 좋네요. 예전처럼 이름 지으면서 머리아플 필요도 없고~

https://junit.org/junit5/docs/5.4.0-RC1/api/org/junit/jupiter/api/MethodOrderer.OrderAnnotation.html

 

MethodOrderer.OrderAnnotation (JUnit 5.4.0-RC1 API)

MethodOrderer that sorts methods based on the @Order annotation. Any methods that are assigned the same order value will be sorted arbitrarily adjacent to each other. Any methods not annotated with @Order will be assigned a default order value of Integer.M

junit.org

이클립스를 사용하는 경우에는 R20181128170323 빌드에는 JUnit 5.3이 포함되어 있어서 해당 API를 사용할 수 없습니다. 3월 RC1 버전을 설치하던가 별도로 JUnit 버전을 올려주어야 합니다.

https://www.eclipse.org/downloads/packages/release/2019-03/m3

 

2019-03 M3 | Eclipse Packages

440 MB 20 DOWNLOADS The Modeling package provides tools and runtimes for building model-based applications. You can use it to graphically design domain models, to leverage those models at design time by creating and editing dynamic instances, to collaborat

www.eclipse.org

 

728x90