views:

138

answers:

4

With JUnit 3, if you forget to add a test to a suite, it will not get run. How can I find all JUnit test cases that are not part of our top level suite, or any suite it recursively contains?

+1  A: 

Using UCDetector, you should be able to identify unused public classes (which include your unused TestCases) from within Eclipse.

springify
+1  A: 

The easiest way to do this is to use the ant junit task and a naming convention for tests (matches *Test.java for example).

Then you can tell junit to run every test in *Test.java in your project. Below is a truncated example taken from the ant manual.

<junit printsummary="yes" haltonfailure="yes">
  <classpath>
    <pathelement location="${build.tests}"/>
    <pathelement path="${java.class.path}"/>
  </classpath>

  <formatter type="plain"/>

  <batchtest fork="yes" todir="${reports.tests}">
    <fileset dir="${src.tests}">
      <include name="**/*Test*.java"/>
      <exclude name="**/AllTests.java"/>
    </fileset>
  </batchtest>
</junit>
Alex B
+1  A: 

What we did on our project was to iterate through all of the class files and see what extended TestCase (you reference the file, do a Class clazz = Class.forName() and then do a Test.isAssignableFrom(clazz) to check.

It definitely got messy (sometimes something is a base class that isn't supposed to be run, for example, and those would have to be tagged).

Knowing if it is in a suite is even harder. I would use my own suite builder class and require all suites be built using it, and add logic to it to record all existing classes that are referenced by the builder, which then passes them to the checker, and the checker knows that they are accounted for.

The problem is that all of this breaks in JUnit4, making it instant legacy code.

The best, most stable solution (until JUnit solves its suite building issues) is to have a development culture that either names all of its tests with a naming conversion (the standard is ending in Test) and has Ant run them, or while developing the test only runs them via the suite.

Yishai
+1  A: 

Code coverage that includes your test package would pick up any test classes that aren't being run

Peter
+1 for the easy way!
furtelwart