2

I know unit tests should be flexible enough to be run in different order. But I have situation where I would want my test cases to run in a specific order. In this scenario I am testing my controllers with fakeApplication and FakeRequest of playframework 2.3. Despite annotating my test class with FixMethodOrder as NAME_ASCENDING my tests run in random order.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JobTest {

    @Test
    public void _1_addJob() {
        running(fakeApplication(), new Runnable() {
            @Override
            public void run() {
            }
        });
    }

    @Test
    public void _2_editJob() {
        running(fakeApplication(), new Runnable() {
            @Override
            public void run() {
            }
        });
    }
}

See below the screenshots where the test orders are swapped with no change in code. Test order 1 Test order 2

rogue-one
  • 11,259
  • 7
  • 53
  • 75
  • I would merge these two test cases into one. – Mon Calamari Feb 19 '15 at 14:47
  • there should be no reason to ask your tests to run in a specified order – Raj Feb 20 '15 at 12:09
  • Yes, even I do not see `@FixMethodOrder(MethodSorters.NAME_ASCENDING)` working as expected. In fact, I see that 2 different threads are invoking my ` test1` and `test2` test cases. I expected the test case execution to be in order and sequential , ie `test1` -> `test2`, and to be run by a single thread. JVM version - 1.8 , junit - 4.12 – Binita Bharati Mar 08 '17 at 16:02

1 Answers1

0

Define the order at the sbt level like this:

testGrouping <<= definedTests in Test map { tests =>
  tests.map { test =>
    import Tests._
    new Group(
      name = test.name,
      tests = Seq(test),
      runPolicy = InProcess)
  }.sortWith(_.name < _.name)
}
czajek
  • 714
  • 1
  • 9
  • 23