Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Ant categorise JUnit tests based on inheritance hierarchy?

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.)

like image 527
Jonik Avatar asked Jun 01 '11 14:06

Jonik


People also ask

Is it possible to group the test cases in JUnit?

JUnit test suites help to grouping and executing tests in bulk. Executing tests separately for all test classes is not desired in most cases. Test suites help in achieving this grouping. In JUnit, test suites can be created and executed with these annotations.

Where inheritance should not be used in Java?

The reason behind this is to prevent ambiguity. Consider a case where class B extends class A and Class C and both class A and C have the same method display(). Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.

Which annotation is used for grouping multiple tests for a method in JUnit?

class) This annotation is helpful whenever we want to test multiple classes at once. In this case we do not need to run each individual class for testing.

Which supports grouping of test cases?

TestNG Groups allow you to perform groupings of different test methods. Grouping of test methods is required when you want to access the test methods of different classes.


2 Answers

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!

like image 72
Jesse Webb Avatar answered Oct 24 '22 04:10

Jesse Webb


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.

like image 23
Shawn Vader Avatar answered Oct 24 '22 05:10

Shawn Vader