I got a python script which takes command line arguments, working with some files.
I\'m writing succeeding tests with py.test
putting this script through its pa
This turned out to be a problem of relative paths confusing coverage when the measured script is run from another directory. Coverage result files ended up in that directory, instead of the root directory of the project.
To solve this, I stopped using pytest-cov
, and used pure coverage
instead. I used full paths instead of relative paths wherever relevant.
So, e.g.
define the environment variable necessary to enable subprocess coverage via export COVERAGE_PROCESS_START=/full/path/to/.coveragerc
.
In the .coveragerc
, the coverage result file is specified via
[run]
data_file = /full/path/to/.coverage
and any --source
and --include
options should use full paths, too.
Then it was possible to get correct coverage measurement.
I encountered the same issue when calling "py.test --cov ..." from tox. I found a hint on this page: http://blog.ionelmc.ro/2014/05/25/python-packaging/ even though it does not mention this explicitly. Using "--develop" for tox will make sure that coverage data gathering is called from the same directory as coverage analysis. This section in tox.ini made it work for me to have a test environment for coverage:
[tox]
envlist = ...,py34,cov
[testenv:cov]
# necessary to make cov find the .coverage file
# see http://blog.ionelmc.ro/2014/05/25/python-packaging/
usedevelop = true
commands = py.test --cov=<MODULE_NAME>
deps = pytest pytest-cov
Another option with tox is to set the PYTHONPATH
in tox.ini
:
[testenv]
setenv =
PYTHONPATH = {toxinidir}
commands =
pytest --cov=<your package>
- codecov
according to this blog: https://thomas-cokelaer.info/blog/2017/01/pytest-cov-collects-no-data-on-travis/
You should add all the __init__.py
files in tests
folder! This solution works for me.