Can Ant categorise JUnit tests based on inheritance hierarchy?
I'm working with a legacy project that has:
- Pure unit tests
- In开发者_如何学运维tegration tests (slow to run; have all sorts of nasty dependencies)
I'm looking for the simplest way to run both types of tests separately with Ant.
I wonder if there's a way to have Ant automatically recognise these two categories based on the inheritance hierarchy:
StringUtilsTest extends TestCase // "pure unit test"
vs
ProductionDBTest extends AbstractTransactionalTesterBase // "integration test"
There's a hierarchy of abstract superclasses that integration tests are based on, but they all come down to some Spring test classes and ultimately AbstractSpringContextTests
which extends junit.framework.TestCase
.
In other words, can I distinguish, in Ant, tests that (indirectly) extend AbstractSpringContextTests
and tests that directly extend TestCase
? Or would I have to manually go through the tests and e.g. put them in separate Categories or TestSuites? (There are many tests so I wouldn't want to do that.)
Resolution: I tried Sean's (very promising) approach, but couldn't get it working (easily). So I ended up going through the tests semi-manually after all, annotating the pure ones (which was the smaller group) using a setup described in this SO question, and running them with Ant like this. (Note that writing a custom TestRunner is not necessary.)
The solution we used for categorizing our JUnit tests was using a custom annotation. You can then use a custom TestRunner with that which can be given a flag or argument as to which test types to run, or all of them.
Sorry, no example code but creating annotations and a TestRunner is pretty basic, give it a try!
The simple way is to name your test classes ending with the test type
Lets say we were testing dates.
DateTest.java (Quick normal tests)
DateSysTest.java (Long running )
Then in there were two targets one the quick unit tests has
<batchtest todir="your dir">
<!--Run unit tests for each class with suffix Test, unless it is SysTest or StressTest-->
<fileset dir="${src}/test"
includes="**/*Test.java"
excludes="**/*StressTest.java **/*SysTest.java" />
</batchtest>
You then create a few ant targets, one for quick tests one for sys tests and one do them all.. Something like that. That is to say if you can rename your test classes.
I haven't used this myself, mind. But I'm aware that you express unit tests to run as a resource collection, and those can be defined with restrictions. And, one possible restriction is "instanceof" which lets you select classes based on superclass. That lets you select the integration tests, and then using the resource collection "difference" function you can select everything else. Not too bad, if not trivial. This works in Ant 1.8+ I think.
精彩评论