Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PytestUnknownMarkWarning: Unknown pytest.mark.xxx - is this a typo?

I have a file called test.py with the following code:

import pytest

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

class TestClass:
    def test_method(self):
        pass

pytest -s test.py passed but gave the following warnings:

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 ===================

Environment: Python 3.7.3, pytest 5.2.4, anaconda3

What is the best way to get rid of the warning message?

like image 349
Jonathan L Avatar asked Mar 23 '20 00:03

Jonathan L


People also ask

What is a pytest mark?

Pytest allows us to use markers on test functions. Markers are used to set various features/attributes to test functions. Pytest provides many inbuilt markers such as xfail, skip and parametrize. Apart from that, users can create their own marker names.

How do I check my pytest warnings?

-r chars show extra test summary info as specified by chars (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed (w)pytest-warnings (a)all. This will allow to show warnings in the report (top portion of the record) will list which pytest plugins use deprecated arguments (in my case bellow):

What is pytest Mark skip?

If you want to skip all test functions of a module, you may use the pytestmark global: # test_module.py pytestmark = pytest. mark. skipif(...) If multiple skipif decorators are applied to a test function, it will be skipped if any of the skip conditions is true.

How do I register a pytest marker?

Registering markers. Registering markers for your test suite is simple: # content of pytest. ini [pytest] markers = webtest: mark a test as a webtest.


3 Answers

To properly handle this you need to register the custom marker. Create a pytest.ini file and place the following inside of it.

[pytest]
markers =
    webtest: mark a test as a webtest.

Next time you run the tests, the warning about the unregistered marker will not be there.

like image 99
gold_cy Avatar answered Oct 13 '22 03:10

gold_cy


without updating pytest.ini, we can ignore warning using --disable-warnings

We can also use --disable-pytest-warnings

Example using your case: pytest -s test.py -m webtest --disable-warnings

like image 24
Balaji K Avatar answered Oct 13 '22 05:10

Balaji K


@gold_cy's answer works. If you have too many custom markers need to register in pytest.ini, an alternative way is to use the following configuration in pytest.ini:

[pytest]
filterwarnings =
    ignore::UserWarning

or in general, use the following:

[pytest]
filterwarnings =
    error
    ignore::UserWarning

the configuration above will ignore all user warnings, but will transform all other warnings into errors. See more at Warnings Capture

test.py (updated with two custom markers)

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")

Use the following commands to run desired tests:

pytest -s test.py -m webtest
pytest -s test.py -m test1
like image 2
Jonathan L Avatar answered Oct 13 '22 04:10

Jonathan L