0
0
PyTesttesting~10 mins

Why markers categorize and control tests in PyTest - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test demonstrates how pytest markers categorize tests and control which tests run. It verifies that only tests with a specific marker run when selected.

Test Code - pytest
PyTest
import pytest

@pytest.mark.slow
def test_slow_feature():
    assert 2 + 2 == 4

@pytest.mark.fast
def test_fast_feature():
    assert 1 + 1 == 2

# Run command: pytest -m slow
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test runner starts and reads test fileTest file with two tests: one marked 'slow', one marked 'fast'-PASS
2Pytest filters tests to run only those marked 'slow' using '-m slow'Only test_slow_feature is selected to run-PASS
3Pytest runs test_slow_featureExecuting test_slow_feature functionAssert 2 + 2 == 4PASS
4Pytest deselects test_fast_feature because it is not marked 'slow'test_fast_feature is deselected-PASS
5Test run completes with only 'slow' tests executedTest report shows 1 passed, 1 deselected, 0 skippedVerify only tests with 'slow' marker ranPASS
Failure Scenario
Failing Condition: Marker name is misspelled or test run command uses wrong marker
Execution Trace Quiz - 3 Questions
Test your understanding
What does the '-m slow' option do in pytest?
ARuns tests in alphabetical order
BRuns all tests except those marked 'slow'
CRuns only tests marked with 'slow'
DSkips all tests
Key Result
Using markers in pytest helps organize tests into groups so you can run only relevant tests, saving time and focusing on specific features.