views:

697

answers:

2

I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests programmatically and run them? Or is there some good way to construct a test suite containing all the test cases without manually listing out every one (all 700+) of them?

I've tried the "New... -> Test Suite" option in Eclipse, but that seems to work only for JUnit 3, identifying tests by their extending from TestCase

The test classes are JUnit 4, so their only distinguishing characteristic is the annotation, no naming convention, no subclassing from TestCase.

Thanks in advance!

+2  A: 

You can do this fairly easily from within maven using the surefire plugin: I usually clean/compile/install my projects from the command line before comparing them for eclipse usage (mvn eclipse:clean eclipse:eclipse) and you can define a test suite in your pom which lists all the tests you want to run en masse every time you run mvn install. You're not calling them programatically, exactly, but you can certainly call them en masse.

davek
Hmm... I don't use Maven, but I'll try it out if it solves my problem. I'll tick this if I find that it works. Thanks!!
alexloh
+1  A: 

Though it does not really solve your immediate problem, I find it a very useful general practice to create suites and suites of suites, e.g. for a package something like PackageFooSuite etc. and assemble these suites in one or more suites again, like ModuleFooSuite and have one top-level suite, like AllTestsSuite. That way it's easy to run both all tests in one step as well as submodule tests for the package I'm currently working on (and have the tests run quicker than if I would always run all of them):

@RunWith(Suite.class)
@Suite.SuiteClasses({ PackageFooSuite.class, PackageBarSuite.class} )
public final class AllTestsSuite {} // or ModuleFooSuite, and that in AllTests
Fabian Steeg
I tend to agree with you. Unfortunately the people who were working on this before me did not and left maybe 700 or more testcases which I am not keen on manually stringing together by hand
alexloh
@alexloh: Yeah, quite understandable. Perhaps it's actually feasible to automate this without too much hassle, like: for each package/directory, create a suite class made of the names of all the files in the current package/directory. If you start from the bottom directory, it might also be relatively simple to include the generated suites for a sub-package in the level above.
Fabian Steeg