0
0
PytestDebug / FixBeginner · 3 min read

How to Fix 'Marker Not Found' Error in pytest Quickly

The 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.

python
import pytest

@pytest.mark.slow
def test_example():
    assert True
Output
pytest warning: Unknown pytest.mark.slow - is this a typo? You can register markers in pytest.ini
🔧

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.

ini and python
[pytest]
markers =
    slow: marks tests as slow to run

# test_sample.py
import pytest

@pytest.mark.slow
def test_example():
    assert True
Output
============================= test session starts ============================= collected 1 item test_sample.py . [100%] ============================== 1 passed in 0.01s ==============================
🛡️

Prevention

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.

Key Takeaways

Register all custom markers in pytest.ini under the markers section.
Add clear descriptions for each marker to improve test readability.
Keep pytest.ini version controlled to share marker definitions across teams.
Check marker spelling carefully to avoid unknown marker warnings.
Always import pytest in test files using markers.