Is there any tool/library that calculate percent of "condition/decision coverage" of python code. I found only coverage.py but it calculates only percent of "statement coverage".
The percentage of conditions within decision expressions that have been evaluated to both true and false.
To achieve 100% condition coverage, your test cases need to demonstrate a true and false outcome for both conditions. For example, a test case where x is equal to 4 demonstrates a true case for both conditions, and a case where x is equal to 7 demonstrates a false case for both conditions.
A Condition is a Boolean expression containing no Boolean operators. A Decision is a Boolean expression composed of conditions and zero or more Boolean operators. A decision without a Boolean operator is a condition. If a condition appears more than once in a decision, each occurrence is a distinct condition.
Coverage.py now includes branch coverage.
For the curious: the code is not modified before running. The trace function tracks which lines follow which in the execution, and compare that information with static analysis of the compiled byte code to find path possibilities not executed.
I don't know of any branch coverage tools for Python, though I've contemplated writing one. My thought was to start with the AST and insert additional instrumentation for each branch point. It's doable, but there are some tricky cases.
For example,
raise SomeException(x)
Branch coverage for this needs to check that SomeException(x) was fully instantiated and didn't raise its own exception.
assert x, "Oh No!: %r" % (x, y)
This needs to check that the text on the right side of the assertion statement is fully evaluated.
return args.name or os.getenv("NAME") or die("no name present")
Each of the first two terms has to be checked for the true/false path, but not the last. In fact, the last might not even return.
There were a lot of cases to worry about and I had no pressing need for it other than curiosity so I didn't go anywhere with it. I was also wondering if I would get a lot of false positives where I would need some way to repress specific warnings.
If you want to try this route, start with Python 2.6 or 3.0. In those releases the AST module is documented and you can create your own AST nodes before generating the code or .pyc file.
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