就我而言,所有测试都运行了,但覆盖率为 0%。
解决方法是:
$ export PYTHONPATH="."
结果正确后。
我在过去的几个py.test
命令中遇到了导入某些东西的问题,而设置PYTHONPATH
env var 是解决方案。这次它也对我有用。
我的真实例子awslogs
首先PYTHONPATH
未设置:
$ py.test --cov=awslogs tests/
========================================= test session starts =========================================
platform linux2 -- Python 2.7.9, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: /home/javl/sandbox/awslogs/github/awslogs, inifile:
plugins: cov-2.2.0
collected 11 items
tests/test_it.py ...........Coverage.py warning: No data was collected.
--------------------------- coverage: platform linux2, python 2.7.9-final-0 ---------------------------
Name Stmts Miss Cover
-------------------------------------------
awslogs/__init__.py 2 2 0%
awslogs/bin.py 85 85 0%
awslogs/core.py 143 143 0%
awslogs/exceptions.py 12 12 0%
-------------------------------------------
TOTAL 242 242 0%
====================================== 11 passed in 0.38 seconds ======================================
结果覆盖率为 0%。
然后我设置PYTHONPATH
:
$ export PYTHONPATH="."
并重新运行测试:
$ py.test --cov=awslogs tests/
========================================= test session starts =========================================
platform linux2 -- Python 2.7.9, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: /home/javl/sandbox/awslogs/github/awslogs, inifile:
plugins: cov-2.2.0
collected 11 items
tests/test_it.py ...........
--------------------------- coverage: platform linux2, python 2.7.9-final-0 ---------------------------
Name Stmts Miss Cover
-------------------------------------------
awslogs/__init__.py 2 0 100%
awslogs/bin.py 85 9 89%
awslogs/core.py 143 12 92%
awslogs/exceptions.py 12 2 83%
-------------------------------------------
TOTAL 242 23 90%
====================================== 11 passed in 0.44 seconds ======================================
现在是覆盖率90%。
警告:操纵PYTHONPATH
可能会产生奇怪的副作用。目前我遇到了问题,该pbr
基础包在构建可分发时创建了 egg 目录,如果PYTHONPATH
设置为“。”,它会自动认为与 egg 相关的包已安装。出于这个原因,我停止使用pytest-cov
并遵循使用coverage
工具的建议。