7

JUnit 4 has @FixMethodOrder(MethodSorters.NAME_ASCENDING) to support test execution in alphabetical order.

Is there any similar functionality introduced in latest JUnit 5 or any other way to achieve this?

I went through some of the similar issue but could not find any solution. So posting this question again to check for a solution.

Thanks

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Arbind Singh
  • 153
  • 1
  • 2
  • 12
  • 3
    Each test should be standalone; neither depend on nor influence other tests. – DwB Apr 27 '18 at 13:44
  • @DwB: You are right. But if your run on your development system and fail on your build system, it is really hard to find out which test caused the problem. Enforcing the same order on each build system makes bugs reproducible. – CoronA Apr 27 '18 at 17:51
  • 2
    The failed assert message should identify the test. – DwB Apr 27 '18 at 17:52
  • 1
    JUnit Jupiter already enforces a consistent order for every test run. So it's already deterministic on all build systems. It's just that you cannot (yet) provide a "custom order". – Sam Brannen Apr 28 '18 at 12:27
  • Thanks for the valuable input. @SamBrannen, i have 4 test method in a class (like 1,2,3,4), it always picks in the order 2,1,4,3. Is there any logic behind this order ? – Arbind Singh Apr 29 '18 at 19:34
  • Yes, there is _logic_ behind the ordering: that's what makes it deterministic. – Sam Brannen Apr 30 '18 at 10:38
  • 1
    That logic can be found here: https://github.com/junit-team/junit5/blob/6b7da8949e8b0f93f7e4f7f2b745ae0988474c9a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java#L1195-L1210 – Sam Brannen Apr 30 '18 at 10:41

4 Answers4

19

I know I'm late but JUnit5 is capable of that.

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestMethodOrder;
        
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestClass{
   //..
}

This Annotation is sorting by the actual method name, not the Displayname.

Ruslan Bes
  • 2,715
  • 2
  • 25
  • 32
HKiel
  • 191
  • 1
  • 4
  • Thanks, that should be the correct answer. There are some other ordering mechanisms as well, check out `org.junit.jupiter.api.MethodOrderer` – Manuel Aug 27 '20 at 08:07
12

Finally, this is now possible.
@TestMethodOrder is avaliable at snapshot version. (5.4)

@TestMethodOrder(OrderAnnotation.class)
class OrderedTestsDemo {

    @Test
    @Order(1)
    void nullValues() {
        // perform assertions against null values
    }

    @Test
    @Order(2)
    void emptyValues() {
        // perform assertions against empty values
    }

    @Test
    @Order(3)
    void validValues() {
        // perform assertions against valid values
    }

}

source: doc and commit

Edward D. Wilson
  • 351
  • 1
  • 4
  • 11
  • 1
    If you want to run a specific test as a last one, without annotating every test with `@Order`, annotate that single test with `@Order(Integer.MAX_VALUE)` – Tomeister Jun 22 '22 at 07:50
5

JUnit issue is still open https://github.com/junit-team/junit5/issues/13 So, right now there is no such possibility.

Yaroslav
  • 446
  • 4
  • 15
  • 3
    The answer is a bit outdated, as there is already a solution in place. Check out the answer: https://stackoverflow.com/a/55626206/1731935 – Manuel Aug 27 '20 at 08:22
1

Unfortunately at the moment there is currently no mechanism in JUnit5 for ordering the execution of tests.

zappee
  • 20,148
  • 14
  • 73
  • 129