JCollector - JUnit test suite collection
I’ve been toying with the idea of writing a JUnit test suite collector. The trouble with Java is that there’s no way you can ask a class loader for all available classes that it knows about. Things are so much easier in Smalltalk where you ask the TestCase class object for “allSubclasses” and get back a collection.
JCollector has to walk directory listings looking for “.class” file name candidates to work out what you’ve compiled. It uses reflection and a few test suite filtering strategies to build three distinct test suites: unit, functional and acceptance. Test cases are automatically classified based on a naming convention. Any test case prefixed with “TestCaseFunctional” goes in AllCollectedFunctionalTests. Similarly, classes with the name prefix “TestCaseAcceptance” get collected into “AllCollectedAcceptanceTests”. Any remaining test cases are placed in “AllCollectedUnitTests”. (The naming conventions can be changed: you’d have two write a new class for each new type of test suite you want to collect. That would involve inheriting and existing class and writing two, one-line methods.)
There’s two important design decisions here:
If we can’t recognise the type of test case by naming convention, we assume it’s a unit test. If AllCollectedUnitTests is then run in a build without the resources needed by functional and acceptance tests, wrongly named functional and acceptance tests will cause the build to fail. This will make it easier to find wrongly classified test cases.
Originally I thought of generating the test suites as text files that are then compiled, but that’s the C programer mentality. If I generated code, I’d have to compile it and make sure I’m running the latest version. Worse still, I’d probably need a separate directory to hold it so that I could clean it safely without any danger of accidentally deleting real code. There’s no reason why you can’t generate a JUnit test suite on demand. Since it’s simpler, faster and much easier to get right, that’s what I decided to do with JCollector.
Here’s the URL:http://sourceforge.net/projects/jcollector/
Sorry Smalltalkers: Yes, I know, in SUnit… Please don’t laugh. I wish I had a real meta-model to work with too.