Skip to content

Coverage issue with python short-circuit for loop #593

Closed
@nedbat

Description

@nedbat

Originally reported by Glen Nelson (Bitbucket: glen_nelson, GitHub: Unknown)


Sample code below was run with:

  • python 3.5.2
  • coverage 4.4.1
  • nose 1.3.7

Consider the following two files:

#!python

#foo.py
def foo(items):
    for item in items:
        v1 = 5 if item > 5 else None
        v2 = item
        if not v1 or not v2:
            continue
        print(v1)
        print(v2)
#!python

#test_foo.py

from unittest import TestCase

from reproduce import foo


class TestFoo(TestCase):
    def test_foo(self):
        foo.foo([1, 2, 3, 4, 5, 6, 7])

We would expect full coverage of 'foo' here from the test. As we can see, when item is 1-5, we should hit the continue, else we should reach the prints.

Running coverage through nosetests was done with the following command

nosetests -P --with-coverage --cover-erase --cover-inclusive --cover-package reproduce --cover-html-dir=test_coverage_unit --cover-html tests/unit/reproduce

...
Name                                            Stmts   Miss  Cover
-------------------------------------------------------------------
reproduce/foo.py                                    8      1    88%

The missing line is the 'continue', due to short-circuting.


Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions