Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cobertura coverage and the assert keyword

My line coverage for unit tests measured by Cobertura is suffering, because I have assert statements which are not covered in tests. Should I be testing assertions, and is there any way to get Cobertura to ignore them so they do not affect my test coverage?

like image 719
Armand Avatar asked Feb 14 '11 17:02

Armand


1 Answers

The line coverage of your Java assert statements should be simply covered by running your test suite with assertions enabled, i.e., giving -ea as argument to the jvm. If you do this, you'll see that cobertura easily reports 100% line coverage if the rest of your lines are covered as well.

Nevertheless, the assert lines will still be colored red, suggesting insufficient coverage. This is because your assertions are usually always true, so you never hit the false branch.

Thus, branch coverage in Cobertura is messed up by using assert, since assert lines will have 50% branch coverage, rendering the overall branch coverage percentage hard to interpret (or useless).

Clover has a nice feature to ignore assert when computing coverage. I haven't seen this feature in any open source Java coverage tool.

If you use assertions in a design-by-contract style, there is no need to add tests that make your Java assert statements fail. As a matter of fact, for many assertions (e.g., invariants, post-conditions) you cannot even create objects that would make them fail, so it is impossible to write such tests. What you can do, though, is use the invariants/postconditions to derive test cases exercising their boundaries -- see Robert Binder's invariant boundaries pattern. But this won't make your assertions fail.

Only if you have a very tricky pre-condition for a given method, you may want to consider writing a test aimed at making the pre-condition fail. But then re-thinking your pre-condition may be a better idea.

like image 184
avandeursen Avatar answered Sep 18 '22 06:09

avandeursen