提问者:小点点

新闻报道如果是py,则py不包括脚本。测试从另一个目录执行它


我得到了一个python脚本,它接受命令行参数,处理一些文件。我正在用py编写成功的测试。测试将此脚本置于其运行状态,并使用子流程执行它。呼叫

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

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

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

最小示例:

我有一个脚本我的脚本。py仅回显文件arg\u文件的内容。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测试中警告模块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 =================================

共3个答案

匿名用户

我在打电话给“py.test时也遇到了同样的问题...”来自毒理。我在这个页面上发现了一个提示:http://blog.ionelmc.ro/2014/05/25/python-packaging/尽管它没有明确提到这一点。使用tox的“开发”将确保覆盖数据收集是从与覆盖分析相同的目录中调用的。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

匿名用户

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

为了解决这个问题,我停止使用pytestcov,而是使用纯coverage。我在相关的地方使用了完整路径而不是相对路径。

因此,例如,通过export coverage\u PROCESS\u START=/full/path/to/定义启用子流程覆盖所需的环境变量。coveragerc。在中。coveragerc,通过

     [run]
     data_file = /full/path/to/.coverage

而且,任何--Source--all选项都应该使用完整路径。然后就有可能得到正确的覆盖测量。

匿名用户

根据这个博客:https://thomas-cokelaer.info/blog/2017/01/pytest-cov-collects-no-data-on-travis/

您应该添加所有的\uuuu init\uuu。py测试文件夹中的文件!这个解决方案对我有效。