33

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

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

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

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

最小的例子:

我得到了一个脚本my_script.py,它只是回显arg_file.txt通过命令行参数提供的文件的内容。在两个不同的测试中,这一次在 a 中调用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,还有更多奇怪的输出 - 覆盖警告No data was collected,当它显然被收集时,在tmpdir测试中警告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 =================================
4

4 回答 4

17

我在从 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
于 2016-01-10T14:51:22.997 回答
4

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

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

因此,例如定义启用子进程覆盖所需的环境变量export COVERAGE_PROCESS_START=/full/path/to/.coveragerc。在 中.coveragerc,覆盖结果文件是通过指定的

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

并且 any--source--includeoptions 也应该使用完整路径。然后就有可能获得正确的覆盖测量。

于 2013-09-16T09:59:01.233 回答
1

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

您应该添加__init__.py文件tests夹中的所有文件!这个解决方案对我有用。

于 2019-08-10T02:16:58.953 回答
0

tox 的另一个选项是设置PYTHONPATHin tox.ini

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

于 2018-01-09T16:03:55.687 回答