How to Fix 'Marker Not Found' Error in pytest Quickly
marker not found error in pytest happens when you use a custom marker without registering it in pytest.ini. To fix this, add your marker under the markers section in pytest.ini so pytest recognizes it during test runs.Why This Happens
This error occurs because pytest does not recognize custom markers unless they are explicitly registered. When you decorate a test with a marker that pytest doesn't know, it raises a MarkerNotFoundWarning or an error.
import pytest @pytest.mark.slow def test_example(): assert True
The Fix
To fix this, create or update a pytest.ini file in your project root. Add a markers section listing your custom markers. This tells pytest to recognize them and avoid warnings or errors.
[pytest]
markers =
slow: marks tests as slow to run
# test_sample.py
import pytest
@pytest.mark.slow
def test_example():
assert TruePrevention
Always register any custom markers you use in pytest.ini before running tests. Use descriptive names and add comments to explain each marker's purpose. This helps avoid marker-related errors and improves test clarity.
Also, keep your pytest.ini under version control so the whole team shares the same marker definitions.
Related Errors
Other marker-related errors include typos in marker names and using markers without importing pytest. Always check spelling and import pytest in your test files.
For example, using @pytest.mark.slowly instead of @pytest.mark.slow causes a similar warning.