What if you could run just the tests you want with a simple tag?
Why Custom markers in PyTest? - Purpose & Use Cases
Imagine you have a big set of tests for your app. You want to run only the tests that check login features today, but you have to open each test file and find those tests manually.
This manual way is slow and tiring. You might miss some tests or run the wrong ones. It's easy to make mistakes and waste time running tests you don't need right now.
Custom markers let you tag tests with labels like 'login' or 'slow'. Then you can tell pytest to run only tests with those tags. This saves time and keeps your testing organized.
def test_login_valid(): # test code def test_profile_update(): # test code # Manually run tests by picking them one by one
import pytest @pytest.mark.login def test_login_valid(): # test code @pytest.mark.profile def test_profile_update(): # test code # Run tests with 'login' marker using: pytest -m login
You can quickly run focused groups of tests, making your testing faster and smarter.
A developer fixes a bug in the payment system and runs only tests marked 'payment' to check the fix without waiting for unrelated tests.
Manual test selection is slow and error-prone.
Custom markers tag tests for easy grouping.
Run only needed tests quickly with markers.
Practice
Solution
Step 1: Understand what custom markers do
Custom markers in pytest are used to tag tests with labels for easier management.Step 2: Identify the purpose of these labels
These labels help run specific groups of tests selectively using the marker name.Final Answer:
To label tests for grouping and selective running -> Option DQuick Check:
Custom markers = test grouping [OK]
- Thinking markers change Python version
- Assuming markers fix test errors automatically
- Confusing markers with test data generation
slow?Solution
Step 1: Recall pytest marker syntax
Custom markers use dot notation: @pytest.mark.markername before the test function.Step 2: Match the correct syntax
@pytest.mark.slow is the correct way to apply the 'slow' marker.Final Answer:
@pytest.mark.slow\ndef test_example(): pass -> Option AQuick Check:
Marker syntax = @pytest.mark.name [OK]
- Using parentheses instead of dot notation
- Using square brackets instead of dot notation
- Placing marker after function name
pytest -m slow?
import pytest
@pytest.mark.slow
def test_a():
assert True
def test_b():
assert True
@pytest.mark.slow
def test_c():
assert False
Solution
Step 1: Understand what
This command runs only tests marked with the 'slow' marker.pytest -m slowdoesStep 2: Identify marked tests in the code
test_a and test_c have @pytest.mark.slow, so only these run.Step 3: Check test results
test_a passes (assert True), test_c fails (assert False).Final Answer:
Runs test_a and test_c; test_c fails, test_a passes -> Option CQuick Check:
pytest -m slow runs marked tests only [OK]
- Assuming all tests run with -m option
- Ignoring test failures in marked tests
- Running unmarked tests by mistake
import pytest
@pytest.mark.slow()
def test_example():
assert True
Solution
Step 1: Check marker syntax
Markers without parameters should not have parentheses; @pytest.mark.slow() is incorrect.Step 2: Correct usage
Use @pytest.mark.slow without parentheses for simple markers.Final Answer:
Markers should not have parentheses unless parameters are used -> Option AQuick Check:
Use parentheses only with marker args [OK]
- Adding empty parentheses to markers
- Using uppercase marker names incorrectly
- Thinking markers require function parameters
database but exclude those also marked as slow. Which pytest command achieves this?Solution
Step 1: Understand marker expressions
pytest supports boolean expressions with markers: and, or, not.Step 2: Formulate the correct expression
To run tests marked 'database' but exclude 'slow', use "database and not slow".Final Answer:
pytest -m "database and not slow" -> Option BQuick Check:
Use boolean logic in -m to include/exclude markers [OK]
- Using 'or' instead of 'and not' to exclude
- Misplacing 'not' before the first marker
- Combining markers without quotes
Ready to prove it?
Pass the test and this pattern moves unknown
