... newer stories
Tuesday, 14. November 2006
ClasspathSuite for JUnit 4
jlink, 22:11h
Stefan Roock made me aware of the fact that Eclipse (3.2) does not have support for JUnit testing in a multi project setting. He points to some code by Björn Martensson which tackles the problem by searching the classpath for test classes. However, the code works for JUnit 3.8 only, so I set up to tackle the problem for JUnit 4.1.
I tried to come up with a JUnit4-like solution, i.e. using the
The mechanism is simple. Just create a new project in Eclipse and add all projects that contain tests you want to run to its build path. Now create a class like that:
P.S.: ClasspathSuite solves another problem (bug?) in the JUnit 4 integration of Eclipse 3.2: test classes derived from an abstract test class which do not have test methods of their own are being ignored by Eclipse's test runner. When using
I tried to come up with a JUnit4-like solution, i.e. using the
RunWith
annotation. You can download my JUnit extension here.The mechanism is simple. Just create a new project in Eclipse and add all projects that contain tests you want to run to its build path. Now create a class like that:
import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.runner.RunWith; @RunWith(ClasspathSuite.class) public class MySuite {}This will execute all JUnit4 testclasses (those containing methods with the @Test annotation) in the projects classpath. By default jar files are being ignored but you can include them in the search for tests by adding another annotation:
import org.junit.extensions.cpsuite.ClasspathSuite.*; ... @IncludeJars(true) public class MySuite...And you don't have to run all tests. Instead you can use another annotation to restrict the tests to run by regex expressions which will be applied onto the class name before adding a test class to the suite:
import org.junit.extensions.cpsuite.ClasspathSuite.*; ... @ClassnameFilters({"mytests.*", ".*Test"}) public class MySuite...The filter patterns work disjunctively; if any one filter matches, the class will be added to the suite of tests.
P.S.: ClasspathSuite solves another problem (bug?) in the JUnit 4 integration of Eclipse 3.2: test classes derived from an abstract test class which do not have test methods of their own are being ignored by Eclipse's test runner. When using
RunWith(ClasspathSuite.class)
you will catch those test classes as well.... link
... older stories