Skip to content
Advertisement

Coverage for one-liner if statement

Static code analyzers for Python do not generate branches in the following case (one-liner if). Is this by design? Even coverage gives 100% coverage even if only one case is tested.

JavaScript

Can anyone please shed some light on this?

Advertisement

Answer

The first comment says “does not create a control structure.” I don’t know what that means. Whether it’s one line or many lines, the compiled byte code is nearly the same:

JavaScript

The reason coverage.py can’t tell you about the one-line case is because of the trace function that Python uses to tell coverage.py what is going on. The trace function is called for each line executed. The trace function doesn’t get events within a line.

A fuller discussion of possible ways to improve this is here: https://github.com/nedbat/coveragepy/issues/509

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement