【问题标题】:pytest and coverage combination does not workpytest 和覆盖率组合不起作用
【发布时间】:2022-02-25 06:35:06
【问题描述】:

我从这里安装了 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,它肯定测试过。

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

【问题讨论】:

  • 使用前缀为“test_”的文件名,但在任何情况下都不要使用“pytest.py”,这会影响实际的“pytest”模块。

标签: python code-coverage pytest


【解决方案1】:

试试:

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

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

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

【讨论】:

  • 如果“pytest.py”是用户模块,它将不起作用。它掩盖了实际的 py.test(因为您执行“import pytest”来导入帮助程序/事物)。
  • Whups... 在我的示例中应该是 test.py。你当然是完全正确的。 py.test 至少会输出一个像样的错误消息,通知你这个问题。
  • 非常感谢。问题是使用 'pytest.py' 作为文件名。更改后,所有测试都通过了。你的回答是对的。命令应该是“py.test --cov-report term --cov=.test.py”
  • 如果有人想知道如何从 doctests 中获取覆盖率(我的一位同事认为这不受支持) -- $ py.test --doctest-modules . --cov-report term --cov=.$ py.test --doctest-modules mymodule --cov-report term --cov=mymodule
猜你喜欢
  • 2022-12-11
  • 1970-01-01
  • 2023-01-12
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2021-01-29
  • 2016-06-22
相关资源
最近更新 更多