python conditional coverage for subexpression

1.1k views Asked by At

I'm trying to find a python code coverage tool which can measure if subexpressions are covered in a statement:

For instance, I'd like to see if condition1/condition2/condtion3 is covered in following example?

if condition1 or condition2 or condition3: x = true_value
2

There are 2 answers

0
James Mills On

The only reasonable answer to this is: There is no current out-of-the-box implementation.

The closest thing which has branch coverage is Ned Batchelder's coverage.py tool.

NB: Implementing this would not be trivial by any means.

As pointed out by @Ira Baxter it is possible to implement.

1
Hubert Kario On

instrumental does provide condition coverage for python applications but it is not maintained anymore, and its source repository on Bitbucket has been deleted. From an issue on the archived repo:

Matthew Desmarais REPO OWNER Hi Arun,

As.you can see, I haven't really worked on this in a couple of years and probably won't again soon.

[...]

2019-06-02

that being said, if your code works on Python 2.7 or one of the earlier Python 3 releases, it may be good enough

While I wasn't able to integrate it with tox, it does work both with pytest and hypothesis on Python 2.7