PytestUnknownMarkWarning:未知 pytest.mark.xxx - 这是一个错字吗?

Jon*_*n L 21 pytest python-3.x anaconda3 pytest-markers

我有一个名为 test.py 的文件,其中包含以下代码:

import pytest

@pytest.mark.webtest
def test_http_request():
    pass

class TestClass:
    def test_method(self):
        pass
Run Code Online (Sandbox Code Playgroud)

pytest -s test.py 通过但给出以下警告:

pytest -s test.py
=============================== test session starts ============================
platform linux -- Python 3.7.3, pytest-5.2.4, py-1.8.0, pluggy-0.13.1
rootdir: /home/user
collected 2 items

test.py ..

=============================== warnings summary ===============================
anaconda3/lib/python3.7/site-packages/_pytest/mark/structures.py:325
  ~/anaconda3/lib/python3.7/site-packages/_pytest/mark/structures.py:325:
    PytestUnknownMarkWarning: Unknown pytest.mark.webtest - is this a typo?  You can register
    custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=============================== 2 passed, 1 warnings in 0.03s ===================
Run Code Online (Sandbox Code Playgroud)

环境:Python 3.7.3、pytest 5.2.4、anaconda3

摆脱警告消息的最佳方法是什么?

aws*_*ice 33

要正确处理此问题,您需要register the custom marker. 创建一个pytest.ini文件并将以下内容放入其中。

[pytest]
markers =
    webtest: mark a test as a webtest.
Run Code Online (Sandbox Code Playgroud)

下次运行测试时,将不会出现有关未注册标记的警告。


小智 8

在不更新 pytest.ini 的情况下,我们可以使用 --disable-warnings 忽略警告

我们还可以使用 --disable-pytest-warnings

使用您的案例的示例: pytest -s test.py -m webtest --disable-warnings