4

Test suites in Junit4 run nicely, but there's a snag here:

@RunWith(Suite.class)
@Suite.SuiteClasses({ A.class, B.class, ...})

If somebody develops a unit test and forgets to include it in Suite.SuiteClasses, that's obviously a problem.

(that's not a burning problem since Ant will catch that later but still)

So I wondered: if you have say "test" folder in Eclipse project and there are some packages with classes in it - is there a way to include them all automatically in junit4 test suite somehow?

(yes you can right-click "test" folder and do Run as Junit but that sometimes fails individual tests for some reason while they individually pass so I do not have much trust in this solution, plus test suites are nice toys to play with ;-)).

LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93

3 Answers3

1

I suggest ClasspathSuite

import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.runner.RunWith;
@RunWith(ClasspathSuite.class)
public class MySuite {}
NamshubWriter
  • 23,549
  • 2
  • 41
  • 59
  • For a Guava fans, I've made a just one file Guava ClassPath Suite, see https://github.com/xmedeko/gcpsuite – xmedeko Apr 23 '13 at 12:35
0

Not an answer but more then comment:

[...] Run as Junit but that sometimes fails individual tests for some reason while they individually pass [...]

The reason is that some tests don't clean up correctly. This should always put you on alert. Try to identify a pair of tests that can't be executed in one "run" and have a close look at the first test. From my own experience: fix those issues as soon as possible (aka: NOW!), otherwise you may run in very deep problems later (typically: complaints from QA guys, like "the tests fail on my environment")

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Thanks for this alert! Although I do wonder why the test suite with all the junit tests runs green while right-click / Run As Junit does not? :-( (I've had such case today) – LetMeSOThat4U Dec 06 '12 at 21:58
0

i don't agree with Andreas_D. it's not because tests don't clean after themselves. good unit tests don't have to clean after themselves. it's because your some of your tests depends on result of another. you need better tests and/or fixtures

however i agree with part 'fix them now!'. you have a serious problem when results of your tests are not reproducible

piotrek
  • 13,982
  • 13
  • 79
  • 165