coverage.py does not cover script if py.test executes it from another directory

前端 未结 4 1482
眼角桃花
眼角桃花 2020-12-25 12:23

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

相关标签:
4条回答
  • 2020-12-25 12:38

    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.

    0 讨论(0)
  • 2020-12-25 12:50

    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
    
    0 讨论(0)
  • 2020-12-25 12:52

    Another option with tox is to set the PYTHONPATH in tox.ini:

    [testenv] setenv = PYTHONPATH = {toxinidir} commands = pytest --cov=<your package> - codecov

    0 讨论(0)
  • 2020-12-25 12:58

    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.

    0 讨论(0)
提交回复
热议问题