OSGi Testsuite: Introducing Classname Filters
OSGi Testsuite is a JUnit test-runner that collects dynamically test classes for execution. It has been published by my fellow Rüdiger about a year ago and proven useful in some projects already. However for gonsole we had to use an ugly patch because version 1.0 only supported .*Test
postfix matching for test class names.
I solved this problem with version 1.1 by introducing an annotation @ClassnameFilters
that uses regular expressions to match arbitrary name patterns. This post explains in short how it works.
OSGi Testsuite
OSGi Testsuite provides a JUnit test runner BundleTestSuite
that can be used to run all tests within a given number of OSGi bundles. To use it annotate a class with @RunWith(BundleTestSuite.class)
and specify the bundles with @TestBundles({"bundle.1", ...})
. When run JUnit will process all classes in the listed bundles, which have a name ending with 'Test'
.
@RunWith( BundleTestSuite.class ) @TestBundles( { "org.example.bundle1", "org.example.bundle2" } ) public class MasterTestSuite {}
Unfortunately the Test postfix fixation has turned out to be a bit too inflexible. Within gonsole we use different postfixes for unit and integration tests. And we do not want the unit tests to be executed within the OSGi Testsuite run. But this distinction is not possible with version 1.0.
ClassnameFilters
Inspired by ClasspathSuite (which works similar to OSGi Testsuite on plain JUnit tests) I introduced an annotation @ClassnameFilters
. This allows to define filters based on regular expressions to match arbitrary test name patterns:
@RunWith( BundleTestSuite.class ) @TestBundles( { "org.example.bundle1", "org.example.bundle2" } ) @ClassnameFilters( { ".*ITest" } ) public class IntegrationTestSuite {}
Processing the example would include all the tests of classes in the listed bundles, which have a name ending with the 'ITest'
postfix. Note that classes with the simple 'Test'
postfix would not be processed.
Furthermore it is possible to specify exclusion patterns using a leading '!'
:
@RunWith( BundleTestSuite.class ) @TestBundles( { "org.example.bundle1", "org.example.bundle2" } ) @ClassnameFilters( { ".*ITest", "!.*FooITest" } ) public class IntegrationTestSuite {}
The given example would now execute all the tests of classes in the listed bundles, which have a name ending with 'ITest'
postfix except for classes whose names end with ‘FooITest’. Simple enough, isn’t it?
Conclusion
OSGi Testsuite has been enhanced with a filter mechanism for dynamic execution of test classes that matches arbitrary name patterns. Filter specification is done easily using the ClassnameFilters
annotation and regular expressions.
The code is available under the Eclipse Public License and hosted on GitHub:
https://github.com/rherrmann/osgi-testsuite
The latest stable version can be obtained from this p2 repository:
http://rherrmann.github.io/osgi-testsuite/repository
Reference: | OSGi Testsuite: Introducing Classname Filters from our JCG partner Frank Appel at the Code Affine blog. |