What if you could run only the tests you need with a simple label?
Why Markers for categorization in Selenium Python? - Purpose & Use Cases
Imagine you have a big set of tests for a website. You want to run only the tests about login today, but you have to open each test file and find those tests manually.
Doing this by hand is slow and easy to mess up. You might forget some tests or run the wrong ones. It feels like searching for a needle in a haystack every time.
Markers let you tag tests with labels like 'login' or 'slow'. Then you can tell your test tool to run only tests with a certain tag. This saves time and avoids mistakes.
def test_login(): # no marker, must find manually pass def test_logout(): pass
@pytest.mark.login def test_login(): pass @pytest.mark.logout def test_logout(): pass
You can quickly run just the tests you want, making testing faster and more organized.
A team working on a shopping site runs only 'payment' tests before a release to be sure checkout works, without waiting for all tests.
Manual test selection is slow and error-prone.
Markers tag tests for easy grouping and running.
Markers help run focused tests quickly and reliably.
Practice
Solution
Step 1: Understand what markers do
Markers are used to label tests so you can group or select them easily.Step 2: Identify the main purpose
This helps run only certain tests or skip some, improving test control.Final Answer:
To label and organize tests for selective running -> Option BQuick Check:
Markers = Organize tests [OK]
- Thinking markers speed up writing tests
- Believing markers fix test errors automatically
- Confusing markers with report generation
smoke to a test function in pytest?Solution
Step 1: Recall pytest marker syntax
Markers use dot notation: @pytest.mark.marker_name above the test.Step 2: Match syntax to options
Only @pytest.mark.smoke\ndef test_example(): pass uses correct dot notation without extra symbols.Final Answer:
@pytest.mark.smoke\ndef test_example(): pass -> Option AQuick Check:
Marker syntax = dot notation [OK]
- Using parentheses instead of dot notation
- Using brackets or arrows incorrectly
- Missing the @ symbol before pytest.mark
pytest -m smoke?
@pytest.mark.smoke def test_login(): pass @pytest.mark.regression def test_checkout(): pass def test_search(): pass
Solution
Step 1: Understand pytest -m option
The -m option runs tests with the given marker only.Step 2: Identify tests with 'smoke' marker
Onlytest_loginhas @pytest.mark.smoke.Final Answer:
Only test_login -> Option AQuick Check:
pytest -m smoke runs smoke tests only [OK]
- Running all tests ignoring markers
- Confusing marker names
- Assuming unmarked tests run with any marker
pytest -m regression. What is the error?
@pytest.mark.regresion def test_payment(): pass
Solution
Step 1: Check marker spelling
The marker used is 'regresion' but command uses 'regression'.Step 2: Understand effect of misspelling
pytest -m regression won't find tests marked 'regresion', so test is skipped.Final Answer:
Marker name is misspelled -> Option CQuick Check:
Marker spelling must match exactly [OK]
- Typos in marker names
- Forgetting @ before pytest.mark
- Assuming pytest runs all marked tests without -m
slow. Which pytest command should you use?Solution
Step 1: Understand pytest marker expressions
To exclude tests with a marker, use -m "not marker_name" syntax.Step 2: Match command to requirement
pytest -m "not slow" uses "not slow" to run all tests except those marked slow.Final Answer:
pytest -m "not slow" -> Option DQuick Check:
Use -m "not marker" to exclude tests [OK]
- Using -m marker to run only those tests
- Using unsupported --skip or --exclude options
- Not quoting the expression with spaces
Ready to prove it?
Pass the test and this pattern moves unknown
