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

前端 未结 4 1512
眼角桃花
眼角桃花 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.

提交回复
热议问题