pythontoxcoverage.py

Recording coverage from a tox-installed wheel


I have a problem similar to Tox 0% coverage, but I do have some coverage. My runtime code is being covered, but my test code is not. I have built an example app that reproduces the problem.

Name                                                             Stmts   Miss  Cover
------------------------------------------------------------------------------------
.tox/py38/lib/python3.8/site-packages/org/app/__init__.py            0      0   100%
.tox/py38/lib/python3.8/site-packages/org/app/app.py                 2      0   100%
.tox/py38/lib/python3.8/site-packages/org/app/test/__init__.py       0      0   100%
.tox/py38/lib/python3.8/site-packages/org/app/test/test_it.py        5      5     0%
------------------------------------------------------------------------------------
TOTAL                                                                7      5    29%

One hypothesis might be that unittest is only actually running src/org/app/test/test_it.py, and never running .tox/py38/lib/python3.8/site-packages/org/app/test/test_it.py. But the unittest command is coverage run --omit 'src/**' -m unittest discover '{envsitepackagesdir}/org/app', which I would expect to cause discovery to look for tests inside the site-packages directory. Here is the whole tox.ini:

[tox]
envlist = py38

[testenv]
deps = coverage
commands_pre = coverage erase
download = true
commands = coverage run --omit 'src/**' -m unittest discover '{envsitepackagesdir}/org/app'
commands_post = coverage report

[coverage:run]
source_pkgs = org.app

[coverage:paths]
source =
    ${TOX_ENV_DIR-src}/org/app
    src/org/app

[coverage:report]
exclude_lines =
    def __repr__
    if 0:
    if TYPE_CHECKING:
    if __name__ == .__main__.:
    if self.debug:
    if settings.DEBUG
    pragma: no cover
    raise AssertionError
    raise NotImplementedError

How can I get coverage to report the test in site-packages as having been run, or get test discovery to actually run that test instead of the same test someplace else?


Solution

  • I figured it out. The problem was due to my mistaken thinking about how coverage handles the source directive. For the commands being run by tox, directly, I need to supply --source explicitly:

    [testenv]
    commands = coverage run --source '{envsitepackagesdir}/org/app' --omit 'src/**' -m unittest discover '{envsitepackagesdir}/org/app'
    

    then, removing the [coverage:paths] section and setting the source in [coverage:run]:

    [coverage:run]
    source = src/org/app
    

    will enable someone to directly run coverage run -m pytest or pytest --cov and get accurate coverage outside of the toxenv. They won't interfere.