python - pytest 和 coverage 组合不起作用

标签 python code-coverage pytest

我从这里安装了 pytest 插件:http://pypi.python.org/pypi/pytest-cov .然后我有一个简单的测试代码:

pytest.py:

class TestNumbers:
    def test_int_float(self):
        assert 1 == 1.0

    def test_int_str(self):
        assert 1 == 1

我尝试使用命令对其进行测试:py.test --cov-report term --cov pytest.py。但它不起作用。即使我给出了 pytest.py 的整个绝对路径,它仍然没有数据可供收集。但是,如果我使用 py.test pytest.py,它肯定测试正常。

我对这个问题很困惑,感谢帮助。

最佳答案

尝试:

py.test --cov-report term --cov=. test.py

--cov 参数采用一个参数说明要覆盖哪些路径。在您的示例中,--cov 将使用 test.py,但是 py.test 没有关于要测试哪些文件的参数。

更新:正如@hpk42 指出的那样,您需要将您的示例称为pytest.py 以外的名称。当我在本地执行此操作时,我将其称为 test.py

关于python - pytest 和 coverage 组合不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13621466/

相关文章:

python - 是否可以在设备上加载 .gpx 轨道以使用 appium 测试跟踪器?

python - NumPy:有没有办法在 ndarray 上执行 'shifting' ?

python - 如何从可调用 View 渲染 Jinja2 宏?

python - 使用OpenCV和Python裁剪图像轮廓

python - 使 py.test、coverage 和 tox 一起工作 : __init__. py 在测试文件夹中?

java - 如何识别哪些代码行参与了Java程序的特定执行?

spring-boot - 找不到ID为 'org.sonarqube'的插件

python-3.x - 如何要求 Pytest 在 python 脚本中运行特定测试?

python - 使用 conda 从 Anaconda 导入 numpy 时出错(DLL 加载失败 : The specified module could not be found)

python - Pytest moker 补丁属性 :Error 'function' object has no attribute 'patch'