Why does Cobertura fail to report assert branch path was covered?

霸气de小男生 提交于 2019-12-08 17:41:42

问题


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?


回答1:


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:

  1. Whether a given assert statement was executed with assertion checking enabled or disabled.
  2. Whether the predicate actually evaluates to true or to false.

Thus, a total of four outcomes are possible. The information provided for a given line in a HTML report consists of

  • the outcome for condition 1 (0-2 possibilities taken out of 2, addressing execution with checking enabled or disabled),
  • and the outcome for condition 2 (0-2 possibilities taken out of 2: assertion pass or fail).
  • the overall outcome (0-4 out of 4),

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.




回答2:


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



来源:https://stackoverflow.com/questions/5111910/why-does-cobertura-fail-to-report-assert-branch-path-was-covered

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!