我有一个 python 脚本,它接受命令行参数,处理一些文件。我正在编写成功的测试,py.test
让这个脚本通过它的步伐,用subprocess.call
.
现在我想用coverage.py
. 覆盖,当通过pytest-cov
插件(内置子进程处理)使用时,从使用' fixture创建的临时测试目录调用它时看不到/覆盖我的脚本。Coverage在它所在的目录中调用它时确实会看到我的脚本(并且文件名参数指向远程路径)。py.test
tmpdir
在这两种情况下,我的测试都通过了!覆盖 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 =================================