如果 py.test 从另一个目录执行它,coverage.py 不覆盖脚本 [英] coverage.py does not cover script if py.test executes it from another directory

查看:69
本文介绍了如果 py.test 从另一个目录执行它,coverage.py 不覆盖脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,它接受命令行参数,处理一些文件.我正在用 py.test 编写成功的测试,让这个脚本按照它的节奏,用 subprocess.call 执行它.

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 paces, executing it with subprocess.call.

现在我想用 coverage.py 分析代码覆盖率.覆盖率,当通过 pytest-cov 插件(内置子进程处理)使用时,在临时测试中调用时不会看到/覆盖我的脚本使用 py.testtmpdir 夹具创建的目录.Coverage 确实看到我的脚本在它所在的目录中被调用(并且文件名参数指向远程路径).

Now I want to analyze code coverage with coverage.py. Coverage, when used via the pytest-cov plugin (which has subprocess-handling built-in), does not see/cover my script when it is called from a temporary testing directory created with py.test's tmpdir fixture. Coverage does see my script when it's called in the directory it resides in (and the filename argument points to a remote path).

在这两种情况下,我的测试都通过了!Coverage 3.6、pytest-2.3.5、pytest-cov 1.6,全部来自PyPi.

In both situations, my tests pass! Coverage 3.6, pytest-2.3.5, pytest-cov 1.6, all from PyPi.

问题:即使我的脚本在另一个目录中执行,我怎样才能获得覆盖范围以识别它?这是覆盖范围内的错误,还是无法做到的事情?如果后者,毕竟 tmpdir 是 py.test 的一种库存机制,会感到惊讶...

Question: How can I get coverage to recognize my script even if it's executed in another directory? Is this a bug in coverage, or something which is just not possible to do? Would be surprised if the latter, after all, tmpdir is a stock mechanism of py.test...

最小示例:

我得到了一个脚本 my_script.py,它只是回显了通过命令行参数提供的文件 arg_file.txt 的内容.在两个不同的测试中,这一次在 tmpdir 中调用,一次在脚本的位置中调用.两个测试都通过了,但是在 tmpdir 测试中,我没有得到覆盖信息!

I got a script my_script.py which just echoes the contents of a file arg_file.txt supplied via command-line argument. In two different tests, this is once called in a tmpdir, and once in the script's location. Both tests pass, but the in the tmpdir test, I get no coverage information!

试运行:

~/pytest_experiment$ py.test -s
=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 2 items 

tests/test_in_scriptdir.py 
set_up: In directory /tmp/pytest-52/test_10
Running in directory /home/cbuchner/pytest_experiment
Command: ./my_script.py /tmp/pytest-52/test_10/arg_file.txt
--Contents of arg_file.txt--

.
tests/test_in_tmpdir.py 
set_up: In directory /tmp/pytest-52/test_11
Running in directory /tmp/pytest-52/test_11
Command: /home/cbuchner/pytest_experiment/my_script.py arg_file.txt
--Contents of arg_file.txt--

.

================================= 2 passed in 0.06 seconds =================================

覆盖范围:

~/pytest_experiment$ py.test --cov=my_script.py tests/test_in_scriptdir.py=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items 

tests/test_in_scriptdir.py .
--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name        Stmts   Miss  Cover
-------------------------------
my_script       3      0   100%

================================= 1 passed in 0.09 seconds =================================
~/pytest_experiment$ py.test --cov=my_script.py tests/test_in_tmpdir.py=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items 

tests/test_in_tmpdir.py .Coverage.py warning: No data was collected.

--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name    Stmts   Miss  Cover
---------------------------

================================= 1 passed in 0.09 seconds =================================

文件在这里:https://gist.github.com/bilderbuchi/6412754

有趣的是,当使用 -s 运行覆盖测试时,还有更多奇怪的输出 - 覆盖警告 未收集数据,当显然它被收集时,并且在 tmpdir 测试中警告 Module my_script.py 从未被导入.??

Interstingly, when running the coverage tests with -s, too, there's more curious output - coverage warns that No data was collected, when obviously it was collected, and in the tmpdir test warns that Module my_script.py was never imported.??

~/pytest_experiment$ py.test -s --cov=my_script.py tests/test_in_scriptdir.py
=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items 

tests/test_in_scriptdir.py 
set_up: In directory /tmp/pytest-63/test_10
Running in directory /home/cbuchner/pytest_experiment
Command: ./my_script.py /tmp/pytest-63/test_10/arg_file.txt
--Contents of arg_file.txt--

Coverage.py warning: No data was collected.
.
--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name        Stmts   Miss  Cover
-------------------------------
my_script       3      0   100%

================================= 1 passed in 0.09 seconds =================================
~/pytest_experiment$ py.test -s --cov=my_script.py tests/test_in_tmpdir.py=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items 

tests/test_in_tmpdir.py 
set_up: In directory /tmp/pytest-64/test_10
Running in directory /tmp/pytest-64/test_10
Command: /home/cbuchner/pytest_experiment/my_script.py arg_file.txt
--Contents of arg_file.txt--

Coverage.py warning: Module my_script.py was never imported.
Coverage.py warning: No data was collected.
Coverage.py warning: Module my_script.py was never imported.
Coverage.py warning: No data was collected.
.Coverage.py warning: No data was collected.

--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name    Stmts   Miss  Cover
---------------------------

================================= 1 passed in 0.09 seconds =================================

推荐答案

当被测脚本从另一个目录运行时,这被证明是相对路径混淆覆盖的问题.覆盖结果文件最终位于该目录中,而不是项目的根目录中.

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.

为了解决这个问题,我停止使用 pytest-cov,而是使用纯 coverage.我在任何相关的地方使用完整路径而不是相对路径.

To solve this, I stopped using pytest-cov, and used pure coverage instead. I used full paths instead of relative paths wherever relevant.

所以,例如通过 export COVERAGE_PROCESS_START=/full/path/to/.coveragerc 定义启用子进程覆盖所需的环境变量.在.coveragerc中,通过

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

和任何 --source--include 选项也应该使用完整路径.这样就有可能得到正确的覆盖测量.

and any --source and --include options should use full paths, too. Then it was possible to get correct coverage measurement.

这篇关于如果 py.test 从另一个目录执行它,coverage.py 不覆盖脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆