Markers help you group and label tests so you can run or skip certain tests easily.
Markers for categorization in Selenium Python
Introduction
Syntax
Selenium Python
@pytest.mark.marker_name def test_function(): pass
Use @pytest.mark.marker_name above your test function to add a marker.
You can use multiple markers by stacking them.
Examples
Selenium Python
@pytest.mark.smoke def test_login(): assert True
Selenium Python
@pytest.mark.skip def test_not_ready(): assert False
Selenium Python
@pytest.mark.database @pytest.mark.slow def test_data_load(): assert True
Sample Program
This script has three tests with different markers. The 'test_skip_example' will be skipped. You can run tests by marker using pytest options.
Selenium Python
import pytest @pytest.mark.smoke def test_addition(): assert 1 + 1 == 2 @pytest.mark.skip def test_skip_example(): assert False @pytest.mark.slow def test_long_process(): assert sum(range(5)) == 10
Important Notes
To run tests with a specific marker, use pytest -m marker_name.
To skip tests with a marker, use pytest -m 'not marker_name'.
Remember to register custom markers in pytest.ini to avoid warnings.
Summary
Markers label tests to organize and control which tests run.
You add markers using @pytest.mark.marker_name above test functions.
Markers help run, skip, or group tests easily.
Practice
1. What is the main purpose of using markers in pytest with Selenium tests?
easy
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]
Hint: Markers help pick which tests to run or skip [OK]
Common Mistakes:
- Thinking markers speed up writing tests
- Believing markers fix test errors automatically
- Confusing markers with report generation
2. Which is the correct way to add a marker named
smoke to a test function in pytest?easy
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]
Hint: Use dot notation: @pytest.mark.marker_name [OK]
Common Mistakes:
- Using parentheses instead of dot notation
- Using brackets or arrows incorrectly
- Missing the @ symbol before pytest.mark
3. Given this pytest code snippet, what tests will run if you execute
pytest -m smoke?
@pytest.mark.smoke def test_login(): pass @pytest.mark.regression def test_checkout(): pass def test_search(): pass
medium
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]
Hint: -m runs tests with matching marker only [OK]
Common Mistakes:
- Running all tests ignoring markers
- Confusing marker names
- Assuming unmarked tests run with any marker
4. You wrote this test with a marker but it does not run when you use
pytest -m regression. What is the error?
@pytest.mark.regresion def test_payment(): pass
medium
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]
Hint: Check marker spelling matches command exactly [OK]
Common Mistakes:
- Typos in marker names
- Forgetting @ before pytest.mark
- Assuming pytest runs all marked tests without -m
5. You want to run all tests except those marked
slow. Which pytest command should you use?hard
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]
Hint: Use -m "not marker" to skip marked tests [OK]
Common Mistakes:
- Using -m marker to run only those tests
- Using unsupported --skip or --exclude options
- Not quoting the expression with spaces
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
