Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using pytest-timeout in test cases?
easy
A. To stop tests that run longer than a specified time limit
B. To increase the speed of test execution by skipping tests
C. To automatically retry failed tests
D. To generate detailed test coverage reports

Solution

  1. Step 1: Understand pytest-timeout functionality

    pytest-timeout is designed to stop tests that exceed a time limit to prevent long waits.
  2. Step 2: Compare options with pytest-timeout purpose

    Only To stop tests that run longer than a specified time limit matches this purpose; others describe different pytest features.
  3. Final Answer:

    To stop tests that run longer than a specified time limit -> Option A
  4. Quick Check:

    pytest-timeout stops long tests = C [OK]
Hint: Remember: pytest-timeout stops tests running too long [OK]
Common Mistakes:
  • Confusing timeout with test retries
  • Thinking it speeds up tests by skipping
  • Mixing it with coverage reporting
2. Which of the following is the correct way to set a 5-second timeout for a test function using pytest-timeout?
easy
A. @pytest.mark.timeout(seconds=5) def test_example(): pass
B. @pytest.timeout(5) def test_example(): pass
C. @pytest.mark.timeout(5) def test_example(): pass
D. @pytest.timeout(seconds=5) def test_example(): pass

Solution

  1. Step 1: Recall correct pytest-timeout decorator syntax

    The correct decorator is @pytest.mark.timeout(5) using a positional argument.
  2. Step 2: Check each option's syntax

    @pytest.mark.timeout(5) def test_example(): pass uses correct syntax with positional argument 5. @pytest.mark.timeout(seconds=5) def test_example(): pass incorrectly uses keyword argument 'seconds=5'. The options using @pytest.timeout use wrong decorator names.
  3. Final Answer:

    @pytest.mark.timeout(5) def test_example(): pass -> Option C
  4. Quick Check:

    Use @pytest.mark.timeout(5) = A [OK]
Hint: Use @pytest.mark.timeout(5) to set 5 seconds [OK]
Common Mistakes:
  • Using @pytest.timeout instead of @pytest.mark.timeout
  • Passing seconds as a keyword argument
  • Missing the @pytest.mark prefix
3. Given the following test code, what will happen when running pytest?
@pytest.mark.timeout(2)
def test_sleep():
    import time
    time.sleep(3)
medium
A. The test will pass because sleep is allowed
B. The test will raise a syntax error
C. The test will be skipped automatically
D. The test will fail due to exceeding the 2-second timeout

Solution

  1. Step 1: Analyze the timeout and sleep duration

    The test has a 2-second timeout but sleeps for 3 seconds, exceeding the limit.
  2. Step 2: Understand pytest-timeout behavior on timeout

    pytest-timeout stops the test and marks it as failed if it runs longer than the timeout.
  3. Final Answer:

    The test will fail due to exceeding the 2-second timeout -> Option D
  4. Quick Check:

    Sleep 3s > timeout 2s = fail [OK]
Hint: If test runs longer than timeout, it fails [OK]
Common Mistakes:
  • Assuming test passes despite timeout
  • Thinking test is skipped automatically
  • Confusing timeout with syntax errors
4. Identify the error in the following pytest test using pytest-timeout:
@pytest.mark.timeout(3)
def test_loop():
    for i in range(5):
        time.sleep(1)
medium
A. Timeout value is too low for the loop
B. Missing import of the time module
C. Incorrect decorator syntax
D. The test will pass without errors

Solution

  1. Step 1: Check for missing imports

    The test uses time.sleep(1) but does not import the time module.
  2. Step 2: Verify other aspects

    The timeout and decorator syntax are correct. The missing import will cause a NameError.
  3. Final Answer:

    Missing import of the time module -> Option B
  4. Quick Check:

    Using time.sleep without import = error [OK]
Hint: Always import modules before using their functions [OK]
Common Mistakes:
  • Ignoring missing imports
  • Assuming timeout causes error instead
  • Thinking decorator syntax is wrong
5. You want to apply a 10-second timeout to all tests in a pytest file without adding @pytest.mark.timeout to each test. Which is the best way to do this?
hard
A. Add pytestmark = pytest.mark.timeout(10) at the top of the test file
B. Set timeout=10 in pytest.ini configuration file
C. Use @pytest.mark.timeout(10) on a dummy test and inherit it
D. Add timeout=10 as a command line argument every time

Solution

  1. Step 1: Understand pytestmark usage

    Setting pytestmark = pytest.mark.timeout(10) applies the timeout decorator to all tests in the file automatically.
  2. Step 2: Evaluate other options

    Adding timeout as a command line argument requires specifying --timeout=10 every time pytest is run; setting timeout in pytest.ini applies it globally to all tests in the project; using a dummy test for inheritance is not a valid pytest feature. pytestmark is best for this specific file.
  3. Final Answer:

    Add pytestmark = pytest.mark.timeout(10) at the top of the test file -> Option A
  4. Quick Check:

    pytestmark applies decorator to all tests [OK]
Hint: Use pytestmark variable to apply timeout to all tests [OK]
Common Mistakes:
  • Trying to set timeout in pytest.ini (global, not file-specific)
  • Using dummy test inheritance (not valid)
  • Relying on command line timeout argument (inconvenient)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard