我有一个名为test.py的文件,其中包含以下代码:
import pytest
@pytest.mark.webtest
def test_http_request():
pass
class TestClass:
def test_method(self):
pass
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 ===================
环境: Python 3.7.3,pytest 5.2.4,anaconda3
消除警告消息的最佳方法是什么?
发布于 2020-03-23 20:08:43
要正确处理此问题,您需要register the custom marker
。创建一个pytest.ini
文件,并将以下内容放入其中。
[pytest]
markers =
webtest: mark a test as a webtest.
下次运行测试时,将不会出现有关未注册标记的警告。
发布于 2020-08-16 13:01:19
在不更新警告的情况下,我们可以使用-- pytest.ini -warnings忽略警告
我们还可以使用--disable-pytest-warnings
使用您的案例的示例: pytest -s test.py -m webtest --disable-warnings
发布于 2020-05-02 15:20:09
@gold_cy的答案是有效的。如果您有太多的自定义标记需要在pytest.ini中注册,另一种方法是在pytest.ini中使用以下配置:
[pytest]
filterwarnings =
ignore::UserWarning
或者一般情况下,使用以下命令:
[pytest]
filterwarnings =
error
ignore::UserWarning
上面的配置将忽略所有用户警告,但会将所有其他警告转换为错误。欲了解更多信息,请访问Warnings Capture
test.py (使用两个自定义标记进行更新)
import pytest
@pytest.mark.webtest
def test_http_request():
print("webtest::test_http_request() called")
class TestClass:
@pytest.mark.test1
def test_method(self):
print("test1::test_method() called")
使用以下命令运行所需的测试:
pytest -s test.py -m webtest
pytest -s test.py -m test1
https://stackoverflow.com/questions/60806473
复制相似问题