What if you could run just the tests you need with one simple command?
Why Running tests by marker (-m) in PyTest? - Purpose & Use Cases
Imagine you have a big set of tests for your app. You want to run only the tests about login, but you have to open each test file and run them one by one.
This manual way is slow and tiring. You might forget some tests or run the wrong ones. It wastes time and can cause mistakes.
Using markers with -m lets you tag tests and run just the ones you want with one simple command. It saves time and keeps things organized.
pytest tests/test_login.py
grep 'def test_login' tests/test_login.py | xargs pytestpytest -m login
You can quickly run focused groups of tests anytime, making testing faster and smarter.
When fixing a bug in the payment system, you run only payment tests marked with @pytest.mark.payment to check your fix without waiting for all tests.
Manual test selection is slow and error-prone.
Markers let you tag and run specific test groups easily.
Running tests by marker speeds up your workflow and reduces mistakes.
Practice
-m option do when running pytest?Solution
Step 1: Understand pytest markers
Markers are labels you add to tests to group them logically.Step 2: Use
The-moption-moption runs only tests that have the specified marker label.Final Answer:
Runs only tests with the specified marker -> Option AQuick Check:
pytest -m runs tests by marker = A [OK]
- Confusing -m with directory or file options
- Thinking -m runs all tests regardless of marker
- Assuming -m runs tests in parallel
slow?Solution
Step 1: Recall pytest command options
The correct option to select tests by marker is-m, not--markeror others.Step 2: Match syntax to run tests with marker
The correct command ispytest -m slowto run tests marked asslow.Final Answer:
pytest -m slow -> Option BQuick Check:
Use single dash -m for marker filter = D [OK]
- Using --marker instead of -m
- Adding extra dashes like --m
- Confusing option names
fast and db:
@pytest.mark.fast def test_a(): pass @pytest.mark.db def test_b(): pass @pytest.mark.fast def test_c(): passWhat tests run with the command
pytest -m "fast and not db"?Solution
Step 1: Understand the marker expression
The command runs tests marked withfastAND NOT marked withdb.Step 2: Check each test's markers
test_aandtest_chavefastonly;test_bhasdbonly.Final Answer:
test_a and test_c -> Option DQuick Check:
fast and not db = test_a, test_c [OK]
- Including tests marked only with db
- Ignoring the 'not' operator
- Assuming all tests run
pytest -m slow but no tests run, even though some tests have @pytest.mark.slow. What is the most likely cause?Solution
Step 1: Check marker registration
Pytest requires custom markers to be registered inpytest.inito avoid warnings and ensure correct filtering.Step 2: Understand effect of missing registration
If markers are not registered, pytest may ignore them or not run tests filtered by them.Final Answer:
Markers are not registered in pytest.ini -> Option AQuick Check:
Unregistered markers cause no tests run = C [OK]
- Confusing -m and --m options
- Assuming tests are skipped by default
- Not checking test file presence
api, ui, and db. You want to run tests that are either api or ui, but not db. Which command achieves this?Solution
Step 1: Understand operator precedence
Logical AND has higher precedence than OR, so parentheses are needed to groupapi or uitogether.Step 2: Analyze each option
pytest -m "(api or ui) and not db" correctly groups(api or ui)and excludesdbwithand not db.Final Answer:
pytest -m "(api or ui) and not db" -> Option CQuick Check:
Use parentheses to group OR conditions = A [OK]
- Ignoring operator precedence without parentheses
- Using AND instead of OR between api and ui
- Including db tests by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
