Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some kind of 'assertion' coverage tool (for Java)?

Before this question is marked as duplicate, please read it. ;) There are already several questions about coverage tools and such, however this is a bit different than the usual ones (I hope).

According to wikipedia there are several different kind of 'coverage' variations that affect several different aspects of the term 'coverage'.

Here a little example:

public class Dummy {
    public int a = 0;
    public int b = 0;
    public int c = 0;

    public void doSomething() {
        a += 5;

        b += 5;

        c = b + 5;
    }
}

public class DummyTest {

    @Test
    public void testDoSomething() {
        Dummy dummy = new Dummy();

        dummy.doSomething();

        assertEquals( 10, dummy.c );
    }
}

As you can see, the test will have a coverage of 100% lines, the assertion on the value of field 'c' will cover this field and indirectly also cover field 'b', however there is no assertion coverage on field 'a'. This means that the test covers 100% of the code lines and assures that c contains the expected value and most probably also b contains the correct one, however a is not asserted at all and may a completely wrong value.

So... now the question: Is there a tool able to analyze the (java) code and create a report about which fields/variables/whatever have not been (directly and/or indirectly) covered by an assertion?

(ok when using getters instead of public fields you would see that getA() is not called, but well this is not the answer I'd like to hear ;) )

like image 879
Danilo Tommasina Avatar asked May 11 '11 07:05

Danilo Tommasina


People also ask

Which of this is Java code coverage tool?

JaCoCo. JaCoCo is an open-source toolkit for measuring and reporting Java code coverage. JaCoCo is distributed under the terms of the Eclipse Public License. It was developed as a replacement for EMMA, under the umbrella of the EclEmma plug-in for Eclipse.

What is coverage test Java?

Test measurement helps in identifying and minimizing bugs and design defects in your code. While there are various methodologies to measure test effectiveness, code coverage is one of the most popular.

Is code coverage a tool?

Cobertura is a code coverage tool. This is actually a code coverage utility tool developed specifically for Java. Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage.


2 Answers

There are hundreds of definition of "test coverage", of which the COTS tools only handle a very few at best. (My company builds test coverage tools so we track this kind of thing). See this lecture on test coverage for an interesting overview.

The closest definition I have heard is one for data coverage; depending on your definition :-{ it tells you that each data item has been written and read during execution. The lecture talks about verifying that every write and every read has been exercised as a special case.

I don't know the hundreds of definitions by heart, but you may have invented yet one more: data coverage restricted to assertions.

like image 33
Ira Baxter Avatar answered Nov 02 '22 22:11

Ira Baxter


As you can see, the test will have a coverage of 100% lines, the assertion on the value of field 'c' will cover this field and indirectly also cover field 'b', however there is no assertion coverage on field 'a'. This means that the test covers 100% of the code lines and assures that c contains the expected value and most probably also b contains the correct one, however a is not asserted at all and may a completely wrong value.

Well, "cover" unfortunately means different things to different people... This test indeed exercises 100% of the code lines, but it does not test them all.

What you're looking for is handled well by mutation testing.

Have a look at Jester, which uses mutation testing to report on code coverage.

like image 70
Don Roby Avatar answered Nov 02 '22 23:11

Don Roby