Don't measure coverage from certain lines

Issue #668 closed
pckroon created an issue

Hi all,

first off, thanks for the great work you've been doing so far with this :)

This is a duplicate of the issue I filed on pytest-cov earlier ( I have a project that parses data files on import, which means that the parser code is always considered as covered, even though it is not actually tested. Is there any way to either record a "background" which is later substracted, or a way to specifically exclude coverage coming from certain lines (i.e. the import statements)?

Minimal example:



from .func import f


def parse_datafile():
    return 8

a = parse_datafile()


def f(x): return x**2


from import a  # pragma: no cover                                  
from project import f                                                           

def test_f():                                                                   
    assert f(a) == a**2                                                         


from import parse_datafile  # pragma: no cover

def test_parse():
    assert parse_datafile() == 8
> coverage erase && pytest --cov=project tests/
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/peterkroon/python/coverage_meuk, inifile:
plugins: cov-2.5.1
collected 1 item                                                               

tests/ .                                                        [100%]

----------- coverage: platform linux, python 3.5.2-final-0 -----------
Name                  Stmts   Miss  Cover
project/       1      0   100%
project/           3      0   100%
project/           2      0   100%
TOTAL                     6      0   100%

I expect project/ to be 0% covered, unless I also run I can't make that work with a .coveragerc or pragmas. Any advice is highly welcome :)

Comments (5)

  1. Ned Batchelder repo owner

    Hmm, this seems like the opposite of the nocover pragma: a line that is executed, but you don't want counted as covered. There is no way to do that now. I'm not sure how you would use it in your scenario, since running should count it as covered.

    Perhaps if #170 (who tests what) is ever implemented, it will give you the information you need?

    Can you say more how you would use this in a real project? Are you trying to make the total percentage more accurate, or are you trying to make the red/green line markers more accurate? Or something else?

  2. pckroon reporter

    Thanks for the lightning fast reply. And indeed, running test_data should count is as covered, but currently running test_x also covers it.

    What I want to use it for is to make sure the parser code is also actually tested and produces the expected output, so it's mostly about the percentages.

  3. Ned Batchelder repo owner

    How do you envision running your tests so that you can run test_data and get the lines counted, and also run other tests and not get the lines counted?

  4. pckroon reporter

    I think (from coverage's perspective) I can make a file like such:


    and then do coverage run where first is ran and recorded, and the lines covered there are substracted from the actual coverage results from the tests. Pytest-cov could wrap this by making the background equivalent to test discovery.

  5. Log in to comment