What if you could run just the tests you need with a simple tag, saving hours of waiting?
Why Marker registration in PyTest? - Purpose & Use Cases
Imagine you have many tests in your project and you want to run only some of them based on categories like 'slow', 'database', or 'api'. Without markers, you have to manually pick and run tests one by one or write complex commands every time.
Manually selecting tests is slow and easy to forget. You might run the wrong tests or miss important ones. It's like trying to find a book in a huge library without any labels or organization.
Marker registration lets you label your tests with clear tags. You register these markers once, so pytest knows about them. Then you can easily run tests by category, making your testing faster and more organized.
def test_api(): # no marker pass def test_db(): # no marker pass # Run tests manually by name or folder
import pytest @pytest.mark.api def test_api(): pass @pytest.mark.database def test_db(): pass # Run tests by marker: pytest -m api
You can quickly run or skip groups of tests by their purpose, saving time and avoiding mistakes.
A team working on a web app uses markers to separate fast unit tests from slow integration tests. Developers run only fast tests during coding, and the full suite runs before release.
Manual test selection is slow and error-prone.
Marker registration labels tests clearly for easy grouping.
Running tests by marker speeds up development and improves accuracy.
Practice
pytest.ini?Solution
Step 1: Understand marker registration role
Markers must be registered inpytest.inito inform pytest about custom markers.Step 2: Identify effect of registration
Without registration, pytest shows warnings about unknown markers; registration prevents these warnings.Final Answer:
To avoid warnings when using custom markers in tests -> Option AQuick Check:
Marker registration = avoid warnings [OK]
- Thinking registration runs tests automatically
- Believing registration disables markers
- Confusing marker registration with test discovery
slow in pytest.ini?Solution
Step 1: Recall pytest.ini marker syntax
Markers are registered under the [pytest] section with the key 'markers' followed by marker descriptions.Step 2: Match correct syntax
[pytest] markers = slow: marks tests as slow correctly uses '[pytest]' section and 'markers = slow: marks tests as slow' format.Final Answer:
[pytest] markers = slow: marks tests as slow -> Option AQuick Check:
Correct section and key = [pytest] markers = slow: marks tests as slow [OK]
- Using wrong section name like [markers]
- Misspelling 'markers' key
- Omitting colon after marker name
pytest.ini content and test code, what will be the result when running pytest -m slow?
[pytest] markers = slow: marks tests as slow
import pytest
@pytest.mark.slow
def test_example():
assert True
def test_fast():
assert True
Solution
Step 1: Understand
The-m slowoption-m slowoption runs only tests marked with@pytest.mark.slow.Step 2: Identify marked tests
test_exampleis marked with@pytest.mark.slow,test_fastis not.Final Answer:
Only test_example runs and passes -> Option CQuick Check:
Run with -m slow = only slow tests run [OK]
- Assuming all tests run with -m option
- Confusing marker name with test function name
- Ignoring marker registration effect
database to your tests but pytest shows a warning: PytestUnknownMarkWarning. What is the most likely cause and how to fix it?Solution
Step 1: Identify cause of PytestUnknownMarkWarning
This warning appears when a custom marker is used but not registered inpytest.ini.Step 2: Fix by registering marker
Adddatabasemarker under the [pytest] section inpytest.inito register it and remove the warning.Final Answer:
You forgot to register the database marker in pytest.ini; add it under [pytest] markers -> Option BQuick Check:
Warning = missing marker registration [OK]
- Ignoring the warning and continuing
- Removing marker decorator instead of registering
- Trying to rename marker instead of registering
api but skip those marked as slow. Your pytest.ini contains:
[pytest]
markers =
api: marks API tests
slow: marks slow tests
Which command correctly runs only api tests excluding slow tests?Solution
Step 1: Understand marker expressions with -m
The-moption accepts boolean expressions combining markers usingand,or, andnot.Step 2: Build expression for api tests excluding slow
We want tests markedapibut notslow, so expression isapi and not slow.Final Answer:
pytest -m "api and not slow" -> Option DQuick Check:
Run api tests excluding slow = pytest -m "api and not slow" [OK]
- Using 'or' instead of 'and not' excludes wrong tests
- Misplacing 'not' operator
- Confusing marker names in expression
Ready to prove it?
Pass the test and this pattern moves unknown
