3

I've got Robolectric test suite for my app.

As I understand, by-default Robolectric runs tests in alphabetical order.

I have tests

testA()
testB()

This tests need to run in reverse order, e.g. testB() changes tested activity state to whatever testA() needs.

How do I specify order in which tests must be ran?

Renaming tests by-hand is no good.

Mikhail Krutov
  • 670
  • 1
  • 5
  • 29
  • 3
    Interdependent tests are extremely fragile. You should consider refactoring your tests to avoid this scenario completely. – stkent Feb 07 '15 at 13:39
  • Hmm. Thanks for the answer. I should prepare my object/activities states for every test separately? – Mikhail Krutov Feb 07 '15 at 13:39
  • 2
    Yes, definitely. That way, there is much less chance that adding new tests (or removing obsolete ones) will cause cascading failures. JUnit allows you to specify setup and teardown methods per test class (annotate with "@Before" and "@After") that are run before/after every individual test (annotated with "@Test") in that class. A common pattern is to use the "@Before" annotated method to re-initialize an instance of the class under test, for example. – stkent Feb 07 '15 at 13:44
  • 1
    See http://junit.sourceforge.net/javadoc/org/junit/Before.html and http://junit.sourceforge.net/javadoc/org/junit/After.html – stkent Feb 07 '15 at 13:45

1 Answers1

2

It's not recommend to use fixed test order but you could try to use @FixMethodOrder(MethodSorters.NAME_ASCENDING) at your test classes.

See also the discussion here How to run test methods in specific order in JUnit4?

Community
  • 1
  • 1
nenick
  • 7,340
  • 3
  • 31
  • 23