Verify custom pytest markers are registered and used correctly
Preconditions (2)
✅ Expected Result: Only the test function marked with @pytest.mark.slow runs successfully, and pytest does not show any warnings about unregistered markers
[pytest]
markers =
slow: marks tests as slow
# test_sample.py
import pytest
def test_fast():
assert 1 + 1 == 2
@pytest.mark.slow
def test_slow():
assert sum([1, 2, 3]) == 6
# Command to run in terminal:
# pytest -m slow
The pytest.ini file registers the custom marker slow under the [pytest] section. This prevents pytest from showing warnings about unregistered markers.
The test file test_sample.py contains two tests: test_fast without any marker and test_slow marked with @pytest.mark.slow.
Running pytest -m slow runs only the tests marked with slow. The assertion verifies the test logic is correct.
This setup ensures marker registration and usage are correct and warnings are avoided.
Add two more custom markers 'fast' and 'database' in pytest.ini and create tests using them. Run tests filtered by each marker.
pytest.ini file?pytest.ini prevents warnings about unknown markers and keeps tests organized.slow in pytest.ini?[markers] section in pytest.ini.= with a description, e.g., slow = marks tests as slow.pytest.ini content:
[markers] api = marks tests as api tests slow = marks tests as slowWhat happens if you run
pytest -m slow?-m option runs tests matching the given marker name.slow is registered and used, pytest -m slow runs only tests marked with @pytest.mark.slow.pytest.ini:
[markers] fast marks tests as fastBut when running tests, pytest shows a warning about unknown marker 'fast'. What is the problem?
= between the marker name and its description.fast marks tests as fast lacks '=', so pytest treats 'fast' as unknown marker.pytest.ini: db for database tests and ui for user interface tests. Which pytest.ini content correctly registers both markers?[markers] section, not [pytest].= with a description, e.g., db = marks tests as database tests.db and ui markers are listed on separate lines with descriptions.Pass the test and this pattern moves unknown