Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find classes without unit tests?

Following the standard convention I keep my source code and unit tests in separate folders. I follow the same package structure when creating the test.

For example, if there is a class OrderGenerator in package com.kshitiz in folder src my unit test would be OrderGeneratorTest in com.kshitiz in folder test.

In my current setup this type of organization becomes unmanageable after a while. I cannot write the unit tests before the code. Some other developer is responsible for writing the code and I am supposed to followup with test cases. (Yes I know it should be the other way around but I can't help it)

Since the test cases are written second I must go through all the packages looking for classes and look for methods in those classes to ensure that I've got a test for them. With 100s of classes and 1000s of methods its quite painful.

Is there a way to make sure that your test case coverage is in line with the code? Is there a simple way to find the classes that don't have a corresponding test case somewhere?

I am using Java, Eclipse and Junit 4.

like image 449
Kshitiz Sharma Avatar asked Sep 18 '12 06:09

Kshitiz Sharma


2 Answers

EclEmma

EclEmma is a free Java code coverage tool for Eclipse, available under the Eclipse Public License. It brings code coverage analysis directly into the Eclipse workbench:

  • Fast develop/test cycle: Launches from within the workbench like JUnit test runs can directly be analyzed for code coverage.
  • Rich coverage analysis: Coverage results are immediately summarized and highlighted in the Java source code editors.
  • Non-invasive: EclEmma does not require modifying your projects or performing any other setup.

Code coverage will tell you not only which classes are tested (directly or indirectly) by your tests but also which methods and within those, which branches are covered by your tests. It can be a really great guide to figure out where to spend early testing effort.

Here's the kind of report it gives you:

enter image description here

You can see in the panel at the bottom right how much of each class is covered, so the class second from the bottom is hardly tested at all. At the top right, it shows which branches are covered -- obviously that method has never been tested with empty inputs.

like image 125
Mike Samuel Avatar answered Oct 12 '22 15:10

Mike Samuel


Depending on the nature of your project, you may be interested in Clover, also available as an eclipse plugin. More information at the Atlassian web-site.

like image 20
tucuxi Avatar answered Oct 12 '22 14:10

tucuxi