0
0
PyTesttesting~20 mins

pytest-timeout for time limits - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timeout Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What happens when a test exceeds the timeout?

Consider this pytest test using pytest-timeout to limit execution time to 1 second.

import time
import pytest

@pytest.mark.timeout(1)
def test_sleep():
    time.sleep(2)

What is the test result when you run this?

PyTest
import time
import pytest

@pytest.mark.timeout(1)
def test_sleep():
    time.sleep(2)
AThe test fails due to timeout after 1 second.
BThe test passes because sleep is allowed to finish.
CThe test raises a SyntaxError because of incorrect decorator usage.
DThe test is skipped automatically by pytest.
Attempts:
2 left
💡 Hint

Think about what pytest-timeout does when a test runs longer than allowed.

assertion
intermediate
2:00remaining
Which assertion correctly checks timeout exception?

You want to test that a function long_task() times out after 1 second using pytest-timeout. Which assertion correctly verifies the timeout?

PyTest
import pytest

@pytest.mark.timeout(1)
def test_long_task():
    long_task()
A
with pytest.raises(pytest.TimeoutException):
    long_task()
Bassert long_task() == 'timeout'
Cassert long_task() is None
D
with pytest.raises(TimeoutError):
    long_task()
Attempts:
2 left
💡 Hint

Check which exception pytest-timeout raises on timeout.

🔧 Debug
advanced
2:00remaining
Why does the timeout not stop the test?

This test uses pytest-timeout but the test runs longer than the timeout without stopping:

import time
import pytest

@pytest.mark.timeout(1)
def test_wait():
    time.sleep(3)

What is the most likely reason the timeout is ignored?

AThe test is run with <code>pytest --timeout=0</code> which disables timeout.
BThe <code>@pytest.mark.timeout</code> decorator is misspelled.
CThe <code>pytest-timeout</code> plugin is not installed or not active.
DThe test function name does not start with 'test_'.
Attempts:
2 left
💡 Hint

Check if the plugin is installed and enabled.

framework
advanced
2:00remaining
How to set a global timeout for all tests?

You want to apply a 2-second timeout to all tests in your pytest suite without adding decorators to each test. Which is the best way?

AAdd <code>--timeout=2</code> option in pytest command line or pytest.ini config.
BSet <code>timeout=2</code> inside each test function body.
CAdd <code>@pytest.mark.timeout(2)</code> to each test function manually.
DUse <code>pytest.set_timeout(2)</code> in conftest.py.
Attempts:
2 left
💡 Hint

Think about pytest configuration options.

🧠 Conceptual
expert
2:00remaining
What is the effect of timeout_method=thread in pytest-timeout?

pytest-timeout supports different timeout methods. What happens when you set timeout_method=thread?

AThe test process is forcibly killed immediately on timeout.
BThe test is stopped by raising an exception in a separate thread without killing the process.
CTimeout is ignored and tests run without limits.
DThe test is paused and resumed after timeout.
Attempts:
2 left
💡 Hint

Consider how threads can interrupt test execution.