Report incorrect with branch coverage

Issue #176 invalid
memedough created an issue

using coverage 3.5.1

coverage run test_foo.py

contents is (it won't do anything but define functions, just example file at hand)

import sys

def pytest_generate_tests(metafunc):
    for i in range(10):
        metafunc.addcall()

def test_foo():
    version = sys.version_info[:2]
    if version == (2, 4):
        a = True
    if version == (2, 5):
        a = True
    if version == (2, 6):
        a = True
    if version == (2, 7):
        a = True
    if version == (3, 0):
        a = True
    if version == (3, 1):
        a = True
    if version == (3, 2):
        a = True

do report on .coverage data

coverage report

give

Name       Stmts   Miss Branch BrPart  Cover
--------------------------------------------
test_foo      20     17     16      0    53%

why 53% when only 3 lines covered?

do with branch off get

Name       Stmts   Miss  Cover
------------------------------
test_foo      20     17    15%

is better 15%

Comments (8)

  1. Kevin Qiu

    Running the test file against TIP

    ☿ [kevin@antares] (py: env) ~/src/coverage.py
    [default:2c5fb3a8b81c] $ coverage --version
    Coverage.py, version 3.6.1a0.  http://nedbatchelder.com/code/coverage/3.6.1a0
    
    ☿ [kevin@antares] (py: env) ~/src/coverage.py
    [default:2c5fb3a8b81c] $ coverage run test_foo.py 
    
    ☿ [kevin@antares] (py: env) ~/src/coverage.py
    [default:2c5fb3a8b81c] $ coverage report
    Name       Stmts   Miss  Cover
    ------------------------------
    test_foo      20     17    15%
    
  2. Ned Batchelder repo owner

    Kevin, I'm not sure what you've just posted here? It looks like you are confirming that the original report is still accurate? The OP had a final line that looks just like yours.

  3. Kevin Qiu

    @ned I cannot reproduce the OP's first output. coverage run test_foo.py outputs the result without BrPart, and the result seems correct (3 out of 20 is ~ 15%).

  4. Ned Batchelder repo owner

    @kevinjqiu Then it sounds like you aren't using branch coverage. Note that your output is identical to the OP's output with "branch off" at the end of his description.

  5. Kevin Qiu

    Here's the output when I have --branch option on.

    $ coverage report
    Name       Stmts   Miss Branch BrMiss  Cover
    --------------------------------------------
    test_foo      20     17     16     16     8%
    

    which is correct given the definition of coverage (num of executed lines + num of executed branches) / (total lines + total branches)

  6. Log in to comment