Why is Cobertura not reporting that the claimed branch path has been covered?

In Cobertura, I cannot get him to report that the conditional assert statement path has been executed. Is this a known limitation?

I have a JUnit test that is expecting and an AssertionError will be thrown and it will pass correctly. The problem is that Cobertura reports that the claim branch was not covered.


After more research, I see that part of the branch cover is showing up. Question:

assert data != null; 

and Cobertura reports this as:

Conditional coverage 75% (3/4) [each condition 50%, 100%].

What are the different branching conditions that Cobertura expects?

+7
source share
2 answers

I was able to get 100% coverage by running JUnit twice; once with claims enabled and once with claims disabled.

+3
source

I ran into the same problem, so I spent a bit of time redesigning the answer by donating it to Stack Overflow.

For each Java assert -statement, Cobertura tracks two conditions:

  • Does this assert statement run with a check for enable or disable?
  • Whether the predicate determines true or false.

Thus, four results are possible. The information provided for this line in the HTML report consists of

  • result for condition 1 (0-2 possibilities are taken from 2, addressing with verification is enabled or disabled),
  • and the result for condition 2 (0-2 opportunities taken from 2: affirmation or failure).
  • overall result (0-4 of 4),

Typical scenario:

  • Launch Cobertura once, while approval verification is disabled. You'll get:
    Enabled / Disabled: 50% (disabled); Passed / Failed: 0% (not reached); Consequently, a total of 25%.
    Cobertura will report this as

    Conditional coverage 25% (1/4) [each condition 50%, 0%]

  • Launch Cobertura once, with validation approval enabled. Usually your statements are always true, so you get:
    Enabled / Disabled: 50% (Enabled); Passed / Failed: 50% (always true); Therefore, in general: 50%.

  • Launching Cobertura twice, once with claims verification enabled and once. Assuming the statements are always true, we get:
    Enabled / Disabled: 100% (both on and off); Passed / Failed: 50% (always true); Consequently, a total of 75%.

Then, if we add test cases that guarantee that this statement fails at least once and passes at least once, we get all the numbers 100%.

Please note, however, that if you use assertions in the design style of the contract, you will usually not even be able to make them fail, see Cobertura's answer to another overflow question and the assert keyword .

Finally: although the abstract numbers are explainable, I am not sure that they are very useful. My preference would be to omit the assertion cover from the general reports. Clover can do this, but I don’t know about an open source coverage analysis tool with this nice feature.

+10
source

All Articles