In Cobertura, I can not get it to report that the conditional path of an assert statement was taken. Is this a known limitation?
I have a JUnit test that expects and AssertionError to be thrown, and it passes correctly. The problem is that Cobertura reports that the assert branch was not covered.
After more investigation, I see that part of the branch coverage is being detected. The line is question is:
assert data != null;
and Cobertura reports the coverages as:
Conditional coverage 75% (3/4) [each condition 50%, 100%].
What are the different branch conditions Cobertura is expecting?
I bumped into the same issue, so I spent a bit of time to reverse engineer the answer, donating it to Stack Overflow.
For each Java assert
-statement, Cobertura keeps track of two conditions:
Thus, a total of four outcomes are possible. The information provided for a given line in a HTML report consists of
Typical scenario's are:
Running Cobertura once, with assertion checking disabled.
You'll get:
Enabled/Disabled: 50% (disabled);
Passed/Failed: 0% (not reached); Hence overall 25%.
Cobertura will report this as
Conditional coverage 25% (1/4) [each condition 50%, 0%]
Running Cobertura once, with assertion checking enabled.
Typically your assertions are always true, hence you get:
Enabled/Disabled: 50% (enabled);
Passed/Failed: 50% (always true); Hence overall: 50%.
Running Cobertura twice, once with assertion checking
enabled, and once without.
Assuming assertions are always true, we get:
Enabled/Disabled: 100% (both enabled and disabled);
Passed/Failed: 50% (always true); Hence overall 75%.
Then, if we add test cases that ensure that a given assertion fails at least once, and passes at least once, we get all numbers at 100%.
Note, however, that if you use assertions in the style of design by contract, you will generally not even be able to make them fail, see the answer to another Stack Overflow question, Cobertura coverage and the assert keyword.
Finally: while theses numbers are explainable, I am not sure that they are very useful. My preference would be to be able to omit assertion-related coverage from the overall reports. Clover can do this, but I'm not aware of an open source coverage analysis tool with this nice feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With