9

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
6
  • 2
    He wants to know if a condition has been evaluated as true, if it has been evaluated as false, and/or whether changing its state would have affected the entire decision (it arguably was seen as controlling the decision result). (See MC/DC coverage for more details). Commented Jan 16, 2014 at 1:00
  • I think implementing this is probably not trivial. There is the coverage by Ned Batchelder -- However it only covers branch coverage and not parts of an expression AFAIK. Commented Jan 16, 2014 at 1:08
  • As noted by this question/answer stackoverflow.com/questions/677219/condition-coverage-in-python -- The coverage.py library/tool implements "branch coverage" -- but I'm not sure that it does ganular coverage of conditions like you are asking. Commented Jan 16, 2014 at 1:16
  • Thanks, James. AFAIK, coverage.py doesn't support my use case here, that's the reason why I'm asking this question. Commented Jan 16, 2014 at 1:19
  • 4
    Branch coverage <> MC/DC coverage. Right, it is not trivial to do this. One must instrument the code to collect the status of the conditions as they are executed; this is ideally done cheaply so that performance isn't badly impacted (there's a lot of data being collected, often in an inner loop!). One also needs to build a post-execution analysis of those condition-statuses to determine if they are causal or not. Finally you need some way to display the answer, because it is in terms of partial lines. (I've built this for Rockwell Relay Ladder logic engines using strong tools). Commented Jan 16, 2014 at 1:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

The bitbucket repo has disappeared also :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.