Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pre-define the running sequences of junit test cases?

I have a test class in java and there are several methods annotated by @Test in it, somehow, i want to Junit run method A before method B when i run the whole tests. Is it possible or necessary?

like image 919
George Avatar asked Mar 07 '11 02:03

George


People also ask

How do you run unit tests in a sequence?

To run Unit tests in a specific order we can use the @FixMethodOrder annotation and the MethodSorters class which has three options: MethodSorters. DEFAULT – Sorts the test methods in a deterministic, but not predictable, order. MethodSorters.

Can we set priority in JUnit?

In general, you can't specify the order that separate unit tests run in (though you could specify priorities in TestNG and have a different priority for each test). However, unit tests should be able to be run in isolation, so the order of the tests should not matter.

Which JUnit annotation allows you to define order of execution for the test methods?

Annotation Type FixMethodOrder. This class allows the user to choose the order of execution of the methods within a test class.


3 Answers

This sort of dependency on test methods is bad design and should be avoided. If there is initialization code in one test method that needs to be done for the next, it should be factored out into a setUp method.

like image 137
Edward Z. Yang Avatar answered Oct 13 '22 08:10

Edward Z. Yang


The problem I have with this is reporting. If you WANT/NEED to see if each test method fails or passes then you're SCREWED.

I understand that you don't want one test to build upon previous tests, But regardless of that, there may be situations that you need it to do this (or you'll increase the complexity of the test by an order of magnitude).

Should the flow of tests in the code be up to the developer of the tests or the developer of the framework ?

Show JUnit test code to 10 java developers, and I'll be willing to bet most will assume that the tests (regardless of anything external) will be run in the order they appear in the test class.

Shouldn't THAT be the default behaviour of JUnit ? (Give me the option of telling it the order instead of JUnit figuring it out" on its own.)

Update: 2014-11-18 The newer version of JUnit supports method sorters

// This saves the tests in alphabetical order @FixMethodOrder(MethodSorters.NAME_ASCENDING)

I would think that you might be able to create your own method sorter if you "really" wanted do do your own specific order.

like image 26
Sir Geek Avatar answered Oct 13 '22 08:10

Sir Geek


Tests should have independent order, but some times we have not what we want. If you have a large legacy project with thousands of tests, and they depends on their execution order, you will have many problems, when, for example you will try to migrate on java 7, because it will shuffle all tests.

You can read more about this problem here:

junit test ordering and java 7

like image 2
kornero Avatar answered Oct 13 '22 09:10

kornero