首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PytestUnknownMarkWarning:未知的pytest.mark.xxx -这是一个拼写错误吗?

PytestUnknownMarkWarning:未知的pytest.mark.xxx -这是一个拼写错误吗?
EN

Stack Overflow用户
提问于 2020-03-23 08:16:03
回答 3查看 15.2K关注 0票数 24

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

代码语言:javascript
运行
复制
import pytest

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

class TestClass:
    def test_method(self):
        pass

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

代码语言:javascript
运行
复制
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

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

EN

回答 3

Stack Overflow用户

发布于 2020-03-23 20:08:43

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

代码语言:javascript
运行
复制
[pytest]
markers =
    webtest: mark a test as a webtest.

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

票数 36
EN

Stack Overflow用户

发布于 2020-08-16 13:01:19

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

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

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

票数 4
EN

Stack Overflow用户

发布于 2020-05-02 15:20:09

@gold_cy的答案是有效的。如果您有太多的自定义标记需要在pytest.ini中注册,另一种方法是在pytest.ini中使用以下配置:

代码语言:javascript
运行
复制
[pytest]
filterwarnings =
    ignore::UserWarning

或者一般情况下,使用以下命令:

代码语言:javascript
运行
复制
[pytest]
filterwarnings =
    error
    ignore::UserWarning

上面的配置将忽略所有用户警告,但会将所有其他警告转换为错误。欲了解更多信息,请访问Warnings Capture

test.py (使用两个自定义标记进行更新)

代码语言:javascript
运行
复制
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")

使用以下命令运行所需的测试:

代码语言:javascript
运行
复制
pytest -s test.py -m webtest
pytest -s test.py -m test1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60806473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档