Running Junit tests in a specific order

When running tests with Junit, per default the test execution order is deterministic. But the order of all the tests is not predictable. But for some reasons, it may be useful to ensure a specific order. E.g. one of your tests is sometimes failing and you have no idea. So maybe your production code is not threadsafe or there are some other effects that influence your test without knowing it. E.g. a cache that is filled with some results by a test running before.

This means data of one test influences another test. The tests are not atomic any more.

The order of your tests is not deterministic at all but it is deterministic depending on the machine your tests are running on. So it might be useful for debugging purpose to enforce another order. Junit is capable to provide this for you. There are three different types of method sorters.

  • @FixMethodOrder(MethodSorters.DEFAULT)
  • @FixMethodOrder(MethodSorters.JVM)
  • @FixMethodOrder(MethodSorters.NAME_ASCENDING)

Using NAME_ASCENDING provides a sorting mechanism that is deterministic not only on the machine the tests are running on. So tests are deterministic on all machines the tests are running on. E.g. your development machine or your build server. This might also help for reordering tests and finding out which test order generates different results.

Normally this feature provided by Junit is not necessary, but for debugging purpose it might be really great. I hope you don’t need it. But for debugging it may help. Furthermore I hope you don’t use it for anything else than debugging. ‘Cause this means there is a problem in your code, or library or … something nasty happened.

Sources

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.