0
0
PyTesttesting~10 mins

pytest-timeout for time limits - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a timeout of 5 seconds for the test function.

PyTest
import pytest

@pytest.mark.timeout([1])
def test_example():
    assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
A10
BNone
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or None as timeout disables the timeout.
Forgetting to import pytest.
2fill in blank
medium

Complete the code to apply a timeout of 3 seconds using a pytest fixture.

PyTest
import pytest

@pytest.fixture(autouse=True)
def set_timeout():
    pytest.timeout = [1]


def test_fast():
    assert True
Drag options to blanks, or click blank then click option'
A0
B10
C3
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Setting timeout to zero disables it.
Not using autouse=True to apply fixture automatically.
3fill in blank
hard

Fix the error in the code to correctly apply a 2-second timeout to the test.

PyTest
import pytest

@pytest.mark.timeout([1])
def test_slow():
    import time
    time.sleep(3)
    assert True
Drag options to blanks, or click blank then click option'
A2
B3
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting timeout equal or greater than sleep time does not cause timeout.
Using negative timeout values.
4fill in blank
hard

Fill both blanks to set a timeout of 4 seconds and handle the timeout exception in the test.

PyTest
import pytest
from pytest_timeout import TimeoutException

@pytest.mark.timeout([1])
def test_timeout_handling():
    try:
        import time
        time.sleep(5)
    except [2]:
        assert True
Drag options to blanks, or click blank then click option'
A4
BTimeoutError
CTimeoutException
DException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching wrong exception type.
Setting timeout longer than sleep time.
5fill in blank
hard

Fill all three blanks to apply a 6-second timeout, skip the test if it times out, and import the correct decorator.

PyTest
import pytest
from pytest_timeout import [1]

@[2].timeout([3])
def test_skip_on_timeout():
    import time
    time.sleep(10)
    assert True
Drag options to blanks, or click blank then click option'
Amark
Btimeout
C6
DTimeoutException
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong names from pytest_timeout.
Using incorrect decorator syntax.