2

I have 2 test classes and a suite that initializes some common resource. There are methods annotated with @BeforeClass in all classes - Suite and both test classes and @AfterClass only in suite.

When I run a suit as a separate test class from Eclipse it works fine, but when I try to run all tests in the project the order is invalid. Eclipse for some reason tries to run the tests first (which fails because resource is not initialized yet) and only then the suit itself. Maven seems to ignore the suit either. How can I configure Maven and Eclipse to run tests in correct order(Suit first) and not to run these test from outside the suite?

It's not the matter of code duplication. The problem is with resource which cannot be initialized and destroyed in rapid succession. That's why I need to have a suite which will initialize the resource only once.

Thanks.

Dima
  • 1,774
  • 5
  • 21
  • 31
  • 3
    This is not an answer to your question as such, but in general you should write your tests so that each test is independent so that tests can be run in isolation and so that the order doesn't matter. – mikej Jul 25 '12 at 14:52
  • I try to, but in that case I cannot initialize the resource more than once. – Dima Jul 25 '12 at 14:55
  • Is it possible to put a check for if the resource has already been initialised and to skip initialisation if it has? – mikej Jul 25 '12 at 14:58

2 Answers2

0

Put the code with @BeforeClass and @AfterClass in each test class.

If you have many classes to test, make the test suite abstract and make sure all your classes that needs tre logic inherits from the test suite.

acsadam0404
  • 2,711
  • 1
  • 26
  • 36
0

Your usage of the Suite to do the teardown is causing you problems, for a number of reasons, but specifically the fact that you can't predict the order in which Eclipse (or Maven) will execute your tests. For a fuller explanation, see my answer to Has JUnit4 begun supporting ordering of test? Is it intentional?, but because Eclipse & maven are finding the classes by reflection (annotations), you can't guarantee in which order the classes will be found. [* you can with maven and runOrder, see point 3 below]

Eclipse (and Maven) treats a Suite just like a Test, it's found and run via the same mechanism, and the order in which they are executed in highly dependent on the JVM.

So, if you have Suite A (which contains Test B and C) and Test D, then you'll execute the following (in an unpredictable order):

A (which will execute B & C)
B
C
D

In general, you should try and maintain a symmetry, doing the setUp/teardown in the same place.

So, you have the following options:

  1. Only use @BeforeClass/@AfterClass (or better @ClassRule, ExternalResource), and remove your test suite. This is your cleanest option, because you're doing the setup for each test class.
  2. Only run the test Suite from Eclipse, and use the Maven Surefire Includes/Excludes to do the same in Maven. Note that if you only ever run the test suite, the setup is better done in the Suite rather than in each class. This gives problems when running just a single test from Eclipse however.
  3. Run the tests in a particular order. This can't be done in Eclipse but can in Maven, see surefire:test runOrder. You'll have to have some naming convention.
  4. Use TestNG, which explicitly supports ordering of classes, and is probably easier to setup with the list of classes to run, but you'll still have the same sort of problems with your setup/teardown.
Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • Well, I see what you saying. There is no real support for suits. Then I don't understand Why Junit bother with suits support if no one knows how to work with it properly? Besides I wouldn't call it a real technological challenge to scan the sources and build a dependency tree before executing tests. – Dima Jul 26 '12 at 08:12