5

I'm working with a legacy project that has:

  • Pure unit tests
  • Integration 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.)

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372

3 Answers3

4

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!

Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
  • 1
    I ultimately went with annotations, so I might accept this, but... I have a problem with your advocation of custom TestRunners. :) Having never written one, it didn't seem *that* straightforward to me, based on googling around a while. Fortunately, custom TestRunner is **not** necessary, as you can use a setup like [this](http://stackoverflow.com/questions/2176570/how-to-run-all-tests-belonging-to-a-certain-category-in-junit-4)! – Jonik Jul 11 '11 at 22:32
2

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.

Shawn Vader
  • 12,285
  • 11
  • 52
  • 61
  • +1 My answer talks about using annotations to differentiate test types. This is good because it won't prevent people from renaming tests because of fear of messign up builds. That being said, naming conventions can be used effectively, and if so, this solution is simpler to understand and easier to implement. – Jesse Webb Jun 08 '11 at 14:25
  • Not what I ended up using this time, but +1 anyway because this is a tried and true (albeit old-school) way of separating test categories which works without hassle with most tools (IDEs, Ant etc). – Jonik Jul 11 '11 at 22:37
0

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.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • Thanks, seems interesting! I'm having problems getting `instanceof` working though... not quite sure how you're supposed to introduce the antlib "org.apache.tools.ant.types.resources.selectors" in the project file - by putting attribute xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors" in the project element or what? Oh well, I'll continue tomorrow. Using Ant 1.8.1 so that shouldn't be a problem. – Jonik Jun 01 '11 at 15:08