1

I am using tox to automatically run my tests using pytest and pytest-cov plugin. However, I'm getting coverage reports for the files I omitted in .coveragerc:

(env) alex@smartalex-pc:~/.repos/codelib/github/project$ tox

[...]

../../../tests/test_module1.py::test_func PASSED  [  3%]

[...]

../../../tests/test_module2.py::test_func PASSED  [100%]

----------- coverage: platform linux, python 3.6.7-final-0 -----------
Name                                                                                                   Stmts   Miss  Cover
--------------------------------------------------------------------------------------------------------------------------
/home/alex/.repos/codelib/github/project/.tox/py36/lib/python3.6/site-packages/package/__init__.py             0      0   100%
/home/alex/.repos/codelib/github/project/.tox/py36/lib/python3.6/site-packages/package/__main__.py             2      2     0%
/home/alex/.repos/codelib/github/project/.tox/py36/lib/python3.6/site-packages/package/application.py         40      0   100%
/home/alex/.repos/codelib/github/project/.tox/py36/lib/python3.6/site-packages/package/core.py                73      0   100%
/home/alex/.repos/codelib/github/project/.tox/py36/lib/python3.6/site-packages/package/user_interface.py      45      0   100%
--------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                                        160      2    99%

It seems that tox does not use my .coveragerc. I tried to explicitly specify the config file with --cov-config={toxinidir}/.coveragerc, but I get the same result again.

Simplified project structure:

package/
    __init__.py
    __main__.py
    application.py
    core.py
    user_interface.py
tests/
    test_module1.py
    test_module2.py
.coveragerc
pytest.ini
setup.py
tox.ini

This is my tox.ini:

[tox]
envlist = py36

[testenv]
changedir = {envtmpdir}
deps = 
    trio
    -r dev-requirements.txt
commands =
    pytest -v {toxinidir}/tests --cov=package --cov-config={toxinidir}/.coveragerc

This is my .coveragerc:

[run]
omit =
    package/__main__.py
    package/__init__.py

This is my pytest.ini:

[pytest]
trio_mode = true

I think this is enough but let me know if you need more output/information.

How can I overcome the issue?

adder
  • 3,512
  • 1
  • 16
  • 28

1 Answers1

2

Change .coveragerc to:

[run]
omit =
    */package/__main__.py
    */package/__init__.py
Moldovan Daniel
  • 1,521
  • 14
  • 23
  • That didn't quite work. I'm not getting full coverage (which I do when I run `pytest -v tests --cov=package`) for my modules, and I also get a bunch of "internals" covered (like `env/lib/python3.6/site-packages/_pytest/nose.py 37 25 32%`) – adder Feb 05 '19 at 14:35
  • I did omit them (`__main__.py` and `__init__.py`), and I specified the package that I want covered with `--cov=package`. – adder Feb 05 '19 at 14:46
  • Try add to ommit `*.tox*` and `*env/*` and `*__main__.py` and `*__init__.py` – Moldovan Daniel Feb 05 '19 at 14:53
  • hmm, adding the glob in front of filenames actually kind of worked, however isn't exactly the solution I was hoping for. It omits all files whereas I was looking to omit just two particular files. – adder Feb 05 '19 at 14:58
  • In order to be more specific you can prefix them with the package `*package/__main__.py` and `*package/__init__.py`. Be aware that the absolute path will be different when running with tox compared with when you running with pytest. If you want to work in both cases you still need to have `*` in the path. – Moldovan Daniel Feb 05 '19 at 16:32
  • 1
    okay, I'll give you an upvote for now and if no one comes up with a better solution, I'll mark it as accepted. Thanks for your help! – adder Feb 05 '19 at 17:15
  • btw I think you should narrow down your answer to just the last part (chaning `.coveragerc`) – adder Feb 05 '19 at 17:16
  • Please change `*package` to `*/package`, since the latter is more explicit about specifying the path. Also, updating `pytest.ini` wasn't necessary, so I would delete that part of the answer, too. – adder Feb 05 '19 at 19:07