python conditional coverage for subexpression
Asked Answered
N

2

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
Narcoma answered 16/1, 2014 at 0:56 Comment(6)
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).Hills
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.Albescent
As noted by this question/answer https://mcmap.net/q/606940/-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.Albescent
Thanks, James. AFAIK, coverage.py doesn't support my use case here, that's the reason why I'm asking this question.Narcoma
@Narcoma As I said -- I don't think there is an implementation of this. You might just have to "write it yourself" and/or "contribute such coverage measures to coverage.py"Albescent
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).Hills
A
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.

Albescent answered 16/1, 2014 at 1:37 Comment(0)
A
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

Ashburn answered 27/10, 2019 at 15:47 Comment(1)
The bitbucket repo has disappeared also :(Drown

© 2022 - 2024 — McMap. All rights reserved.