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 (https://github.com/pytest-dev/pytest-cov/issues/207). 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:

project
|-setup.py
|-project
||-data.py
||-func.py
||-__init__.py
|-tests
||-test_x.py
||-test_data.py

project/init.py:

from .func import f

project/data.py:

def parse_datafile():
    return 8

a = parse_datafile()

project/func.py:

def f(x): return x**2

tests/test_x.py

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

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

tests/test_data.py:

from project.data import parse_datafile  # pragma: no cover

def test_parse():
    assert parse_datafile() == 8
> coverage erase && pytest --cov=project tests/test_x.py
============================= 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/test_x.py .                                                        [100%]

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

I expect project/data.py to be 0% covered, unless I also run test_data.py. 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 test_data.py 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. pckroon reporter

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

    background.py

    import project.data
    

    and then do coverage run --background=background.py where first background.py 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.

  4. Log in to comment