I know this is bad practice, but it needs to be done, or I'll need to switch to testng
. Is there a way, similar to JUnit 3's testSuite, to specify the order of the tests to be run in a class?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Joscarsson and Michael D code in my github repo. I hope they don't mind. I also provide ordered version for Parameterized class. It's already to use as maven dependency
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 overridecomputeTestMethods()
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:Running this test produces:
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.From JUnit version 4.11 onwards, it is possible to influence the order of test execution by annotating your class with
@FixMethodOrder
and specifying any of the availableMethodSorters
. See this link for more details.Using
junit 4.11
the new annotation@FixMethodOrder
allows to set a specific order:I can see several reasons for doing this, especially when using JUnit to run functional tests or test persistent objects. For example, consider an object
Article
which is persisted to some kind of persistent storage. If I would like to test the insert, update and delete functionality on theArticle
object following the unit test principle "all tests should be reorderable and test only a specific part of the functionality", I would have three tests:testInsertArticle()
testUpdateArticle()
testDeleteArticle()
However, to be able to test the update functionality, I would first need to insert the article. To test the delete functionality, I would also need to insert an article. So, in practice, the insert functionality is already tested both in
testUpdateArticle()
andtestDeleteArticle()
. It is then tempting to just create a test methodtestArticleFunctionality()
which does it all, but methods like that will eventually get huge (and they won't just test part of the functionality of theArticle
object).The same goes for running functional tests against for example a restful API. JUnit is great also for these cases if it wasn't for the undeterministic ordering of tests.
That said, I extended Michael D's
OrderedRunner
to use annotations to determine order of tests, just thought I should share. It can be extended further, for example by specifying exactly which tests each test depends on, but this is what I'm using for now.This is how it is used. It avoids the need for naming tests like
AA_testInsert()
,AB_testUpdate()
,AC_testDelete()
, ...,ZC_testFilter()
, etc.No matter how these tests are placed in the file, they will always be run as
order=1
first,order=2
second and lastorder=3
, no matter if you run them from inside Eclipse, using Ant, or any other way.Implementation follows. First, the annotation
Order
.Then, the modified
OrderedRunner
.If you want to run junit tests in order "just as they present in your source code", and don't want to modify your tests code, see my note about this here:
How to run junit tests in order as they present in your source code
But it is really not a good idea, tests must be independent.