Automatically include classes in the Java test suite

Junit4 test suites work well, but there is a catch:

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

If someone is developing a unit test and forgets to include it in Suite.SuiteClasses, this is obviously a problem.

(this is not a burning issue, since Ant will catch it later, but still)

So, I thought: if you say a “test” folder in an Eclipse project and there are several packages with classes in it - is there a way to include them all automatically in the junit4 test suite?

(yes, you can right-click on the “test” folder and run Run as Junit, but sometimes for some reason it is not possible to run separate tests while they are going individually, so I don’t really trust this solution, plus test suites - These are good toys to play with ;-)).

+4
source share
3 answers

I suggest ClasspathSuite

 import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.runner.RunWith; @RunWith(ClasspathSuite.class) public class MySuite {} 
+1
source

Not an answer, but more comments:

[...] Run as Junit, but sometimes for some reason it is not possible to run individual tests when they individually pass [...]

The reason is that some tests do not clean up correctly. It should always be on guard. Try to identify a couple of tests that cannot be performed in one “run” and look carefully at the first test. From my own experience: fix these problems as soon as possible (aka: NOW!), Otherwise you may encounter very deep problems later (usually: complaints from QA pairs, for example, “tests do not work in my environment”)

0
source

I do not agree with Andreas_D. this is not because tests do not clear after themselves. good unit tests do not need to be cleaned after yourself. because your some of your tests depend on the result of another. you need better tests and / or lights

however, I agree with the “fix them now!” part. you have a serious problem when the results of your tests are not reproducible.

0
source

All Articles