How to change tests execution order in JUnit5?

2019-01-27 14:02发布

JUnit4 has @FixMethodOrder annotation which allows to use alphabetical order of test methods execution. Is there analogous JUnit5 mechanism?

标签: junit5
2条回答
Fickle 薄情
2楼-- · 2019-01-27 14:24

No, not yet. For unit tests, execution order should be irrelevant. For more complex tests, JUnit is aiming to provide explicit support - test ordering would be part of that.

查看更多
Emotional °昔
3楼-- · 2019-01-27 14:30

This is now possible using snapshot versions of JUnit 5.

https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-test-execution-order

To control the order in which test methods are executed, annotate your test class or test interface with @TestMethodOrder and specify the desired MethodOrderer implementation. You can implement your own custom MethodOrderer or use one of the following built-in MethodOrderer implementations.

Alphanumeric: sorts test methods alphanumerically based on their names and formal parameter lists.

OrderAnnotation: sorts test methods numerically based on values specified via the @Order annotation.

Using snapshot versions (Gradle):

repositories {
    // sonatype - JUnit snapshots
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots/"
    }
}

dependencies {
    testImplementation "org.junit.jupiter:junit-jupiter-api:5.4.0-SNAPSHOT"
    testImplementation "org.junit.platform:junit-platform-commons:1.4.0-SNAPSHOT"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.4.0-SNAPSHOT"
    testRuntimeOnly "org.junit.platform:junit-platform-engine:1.4.0-SNAPSHOT"
}

Of course snapshots are less stable than releases, so either use it at own risk, download and reference a specific snapshot version, or wait for 5.4.0.

查看更多
登录 后发表回答