0
0
PyTesttesting~10 mins

pytest-timeout for time limits - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that a function completes within a set time limit using pytest-timeout. It verifies the test fails if the function runs too long.

Test Code - pytest
PyTest
import time
import pytest

@pytest.mark.timeout(2)
def test_sleep_short():
    time.sleep(1)  # Sleeps less than timeout, should pass

@pytest.mark.timeout(2)
def test_sleep_long():
    time.sleep(3)  # Sleeps longer than timeout, should fail
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test runner starts and loads test_sleep_shortpytest environment ready, test_sleep_short loaded-PASS
2test_sleep_short executes time.sleep(1)Function running, sleeping for 1 secondCheck if test completes within 2 seconds timeoutPASS
3test_sleep_short finishes before timeoutFunction completed normallyTest passes as execution time < 2 secondsPASS
4Test runner starts and loads test_sleep_longpytest environment ready, test_sleep_long loaded-PASS
5test_sleep_long executes time.sleep(3)Function running, sleeping for 3 secondsCheck if test completes within 2 seconds timeoutFAIL
6Timeout triggers after 2 seconds, test_sleep_long interruptedTest interrupted by pytest-timeout pluginTest fails due to exceeding time limitFAIL
Failure Scenario
Failing Condition: Function execution exceeds the 2 seconds timeout limit
Execution Trace Quiz - 3 Questions
Test your understanding
What causes test_sleep_long to fail?
AIt runs longer than the 2 seconds timeout
BIt raises an exception inside the function
CThe test code has a syntax error
DThe pytest-timeout plugin is not installed
Key Result
Using pytest-timeout helps catch tests that hang or run too long, improving test suite reliability by enforcing time limits.