gpt4 book ai didi

python - 如果 py.test 从另一个目录执行它,coverage.py 不会覆盖脚本

转载 作者:IT老高 更新时间:2023-10-28 21:58:42 25 4
gpt4 key购买 nike

我有一个 python 脚本,它接受命令行参数,处理一些文件。我正在使用 py.test 编写成功的测试,让这个脚本通过它的步伐,用 subprocess.call 执行它。

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

在这两种情况下,我的测试都通过了!覆盖 3.6、pytest-2.3.5、pytest-cov 1.6,均来自 PyPi。

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

简单示例:

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

试运行:

~/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 从未被导入。??

~/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 =================================

最佳答案

我在从 tox 调用“py.test --cov ...”时遇到了同样的问题。我在这个页面上发现了一个提示:http://blog.ionelmc.ro/2014/05/25/python-packaging/即使它没有明确提到这一点。对 tox 使用“--develop”将确保从与覆盖分析相同的目录中调用覆盖数据收集。tox.ini 中的这一部分让我有一个覆盖测试环境:

[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

关于python - 如果 py.test 从另一个目录执行它,coverage.py 不会覆盖脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com