How to Run Test Methods in Specific Order in Junit4

How to run test methods in specific order in JUnit4?

I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?

I'm not sure there is a clean way to do this with JUnit, to my knowledge JUnit assumes that all tests can be performed in an arbitrary order. From the FAQ:

How do I use a test fixture?


(...) The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). (...)

Why is it so? Well, I believe that making tests order dependent is a practice that the authors don't want to promote. Tests should be independent, they shouldn't be coupled and violating this will make things harder to maintain, will break the ability to run tests individually (obviously), etc.

That being said, if you really want to go in this direction, consider using TestNG since it supports running tests methods in any arbitrary order natively (and things like specifying that methods depends on groups of methods). Cedric Beust explains how to do this in order of execution of tests in testng.

How to run test methods in order with Junit

So for tests like these - where the steps are dependent on each other - you should really execute them as one unit. You should really be doing something like:

@Test
public void registerWelcomeAndQuestionnaireUserTest(){
// code
// Register
// Welcome
// Questionnaire
}

As @Jeremiah mentions below, there are a handful of unique ways that separate tests can execute unpredictably.

Now that I've said that, here's your solution.

If you want separate tests, you can use @FixMethodOrder and then do it by NAME_ASCENDING. This is the only way I know.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestMethodOrder {

@Test
public void testA() {
System.out.println("first");
}
@Test
public void testC() {
System.out.println("third");
}
@Test
public void testB() {
System.out.println("second");
}
}

will execute:

testA(), testB(), testC()

In your case:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ThisTestsEverything{

@Test
public void T1_registerUser(){
// code
}

@Test
public void T2_welcomeNewUser(){
// code
}

@Test
public void T3_questionaireNewUser(){
// code
}

}

how to make junit test run methods in order

JUnit makes no promises regarding the order in which your tests are run. The order WILL be different depending on the environment and context in which the tests are run.

For this reason (and others) it is considered very bad test design for ordering to affect the behaviour of your tests. You can use @Before go give you a clean slate to work with and then do any set up for a particular test as part of that test.

The accepted answer for the following question gives a good explanation and links to some useful resources: How to run test methods in specific order in JUnit4?

Running JUnit4 Test classes in specified order

You can do this with test suites. (you can "nest" these, too)

@SuiteClasses({SuiteOne.class, SuiteTwo.class})
@RunWith(Suite.class)
public class TopLevelSuite {}

@SuiteClasses({Test1.class, Test2.class})
@RunWith(Suite.class)
public class SuiteOne {}


@SuiteClasses({Test4.class, Test3.class})
@RunWith(Suite.class)
public class SuiteTwo {}

... And so on. Use the "toplevelsuite" as an entry point, and the tests will execute in the order in which you define the @SuiteClasses array.

As for the methods within a class, you can specify the order they execute in with @FixMethodOrder (as you have already mentioned in your question.

Specifying an order to junit 4 tests at the Method level (not class level)

If you're sure you really want to do this: There may be a better way, but this is all I could come up with...

JUnit4 has an annotation: @RunWith which lets you override the default Runner for your tests.

In your case you would want to create a special subclass of BlockJunit4ClassRunner, and override computeTestMethods() to return tests in the order you want them executed. For example, let's say I want to execute my tests in reverse alphabetical order:

public class OrderedRunner extends BlockJUnit4ClassRunner {

public OrderedRunner(Class klass) throws InitializationError {
super(klass);
}

@Override
protected List computeTestMethods() {
List list = super.computeTestMethods();
List copy = new ArrayList(list);
Collections.sort(copy, new Comparator() {
public int compare(FrameworkMethod o1, FrameworkMethod o2) {
return o2.getName().compareTo(o1.getName());
}
});
return copy;
}
}
@RunWith(OrderedRunner.class)
public class OrderOfTest {
@Test public void testA() { System.out.println("A"); }
@Test public void testC() { System.out.println("C"); }
@Test public void testB() { System.out.println("B"); }
}

Running this test produces:

C
B
A

For your specific case, you would want a comparator that would sort the tests by name in the order you want them executed. (I would suggest defining the comparator using something like Google Guava's class Ordering.explicit("methodName1","methodName2").onResultOf(...); where onResultOf is provided a function that converts FrameworkMethod to its name... though obviously you are free to implement that any way you want.

JUnit 4 Test Execution in the order how they are programmed in class

Short answer: no. IMO it is not even possible because your order of methods is not visible in the generated byte code, but I'm not a byte code expert and may be wrong.

The annotation @TestMethodOrder is part of JUnit Jupiter (JUnit 5) and therefore it does not work with JUnit 4 tests. JUnit 4 has an annotation @FixMethodOrder that allows you to execute test methods in alphabetic order. By prefixing your test names with some String like a_ you can achieve what you want. I know it's ugly.

JUnit run the methods in the same order as in file

There is a very crucial point, why you shouldn't start to invent such requirements.

The idea of unit tests is that each test ... tests a different aspect of your class under test. Aspects that are independent of each other. That is the one side. The other is: unit tests exist to help you to determine problems in your production as quickly as possible.

Meaning: when all of a sudden, a unit test is failing (because somebody changed something somewhere), then you want to find the root cause of this problem as soon as possible. Anything that prevents you from getting to that goal makes your unit test less valuable to you!

Things that make it harder for you to find the foot cause would be:

  1. extensive "externalized" setup - when you start using (too much) inheritance for your test cases
  2. complicated relations between your test methods (like: your idea)

Long story short: unit tests are not meant to be run in any specific order. So, instead of spending a lot of time pushing JUnit into a corner where it doesn't want to be ... step back for a second, and reconsider why you think you need things to be that way.

How to run Junit test classes in a particular order in Maven without creating suits?

The Maven Surfire plugin allows you to specify some order by setting the runOrder element: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#runOrder

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<groups>${testcase.groups}</groups>
<runOrder>reversealphabetical</runOrder>
</configuration>
</plugin>


Related Topics



Leave a reply



Submit