Python pytest和coverage组合不起作用

Python pytest和coverage组合不起作用,python,code-coverage,pytest,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 py

我从这里安装了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没有关于要测试哪些文件的参数


UPDATE:正如@hpk42所指出的,您需要调用示例而不是
pytest.py
。当我在本地执行此操作时,我将其称为
test.py

如果“pytest.py”是用户模块,它将无法工作。它隐藏了实际的py.test(因为您使用“导入pytest”来导入帮助程序/内容)。。。在我的示例中,这就是
test.py
。你当然是对的<代码>py.test至少会输出一条不错的错误消息,通知您这个问题。非常感谢。问题是使用“pytest.py”作为文件名。更改后,所有测试都通过了Ok。你的回答是对的。如果有人想知道如何从doctest(我的一位同事认为这不受支持)获取覆盖率,那么命令应该是“py.test--cov report term--cov=.test.py”--
$py.test--doctest模块--cov报告术语--cov=。
$py.test--doctest modules mymodule--cov报告术语--cov=mymodule
使用前缀为“test_u”的文件名,但在任何情况下都不要使用“pytest.py”,因为它会遮挡实际的“pytest”模块。