Search code examples
pythonpython-3.xpytesttoxcoverage.py

no-data-collected when running tests with tox but not when running tests with pytest


I'm using tox to run unit tests with pytest. When I run the tests with tox, it runs the tests and shows the tree of my src directory but prints CoverageWarning: No data was collected. (no-data-collected). When I run with pytest I get the exact same output except without the error and the coverage numbers get printed as expected. My tox.ini looks like this:

[tox]
envlist = py39

[testenv]
deps = 
    pytest
    pytest-cov
    -rrequirements.txt
    -rrequirements.test

commands = pytest {posargs}

[pytest]
addopts =
    -v
    --cov=src
    tests/

I've tried other solutions mentioned on SO, such as

  • Making sure each directory in src has a __init__.py
  • Making sure the test directory has a __init__.py
  • Removing execute permissions for every python file in src
  • Adding include / omit statements to tox.ini
  • Removing modules in src with "test" in the name

Solution

  • I changed my tox.ini to

    [tox]
    envlist = py39
    
    [testenv]
    deps = 
        pytest
        pytest-cov
        -rrequirements.txt
        -rrequirements.test
    
    commands = pytest --cov=src {posargs:tests}
    
    [pytest]
    pythonpath = ./src
    

    which solved the issue. I guess pytest needed the path to be explicitly passed in.