How could I get code coverage for individual tests in Java

2.3k views Asked by At

I have a test class which has a bunch of test cases.

I would like to measure the coverage of individual test case in the class.

Is there any way I could get a report which maps test case and the coverage.

2

There are 2 answers

2
Ira Baxter On

Our SD Java Test Coverage tool can give you this data.

This tool adds a special class TCV with methods to the program under test:

    TCV.reset();  // resets the test coverage vector
    TCV.dump(); // writes vector to file
    TCV.setBaseName(String); // record vector file name prefix

If the application calls the dump function, a test coverage vector is dumped to a file named with the current timestamp and the supplied BaseName.

You can use these to obtain test-specific vectors by modifying your test execution framework, whatever it is, just slightly. Where ever you invoke the Nth test case, add a call to dump:

    TCV.reset(); // marks everything as "not executed"
    tests.nthTestCase();
    TCV.setBaseName("nthTestCase");
    TCV.dump();

This will produce a time stamped vector that covers just the code executed by the nTestCase.

Individual test case results can be found and displayed by the UI component of the tool. The UI component can also combine all those individual test case coverage results, into an overall test coverage result, to give a traditional "everything executed by all test cases" result. Or, you can compare test case coverage results to see which test cases cover the same code, to help eliminate redundant test cases, etc.

The UI component can also produce a report of lines covered by a test; this can be scripted, so one could produce a line-covered report for all test coverage vectors collected individually.

0
MetaCoder On

Have you tried Clover Clover

I know it's not free but it should give you what you need.