0
0
PyTesttesting~10 mins

Ordering tests for parallel safety in PyTest - Interactive Code Practice

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

Complete the code to mark a test to run serially using pytest.

PyTest
import pytest

@pytest.mark.[1]
def test_database_connection():
    assert connect_to_db() is True
Drag options to blanks, or click blank then click option'
Aorder
Bserial_run
Crun_serial
Dserial
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect marker names like 'serial_run' or 'run_serial'.
Trying to use 'order' which is unrelated.
2fill in blank
medium

Complete the code to ensure tests run in a specific order using pytest-ordering.

PyTest
import pytest

@pytest.mark.order([1])
def test_initialize():
    assert setup_environment() is True
Drag options to blanks, or click blank then click option'
A1
B'first'
C'start'
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of integers for order.
Starting order from 0 instead of 1.
3fill in blank
hard

Fix the error in the test function to avoid parallel execution conflicts.

PyTest
import pytest

@pytest.mark.[1]
def test_modify_shared_resource():
    resource.lock()
    modify_resource()
    resource.unlock()
Drag options to blanks, or click blank then click option'
Aconcurrent
Bparallel
Cserial
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parallel' or 'concurrent' which allow parallel runs.
Using 'async' which is unrelated to test ordering.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that orders tests by priority and skips low priority.

PyTest
test_order = {test: priority for test, priority in tests.items() if priority [1] [2]
Drag options to blanks, or click blank then click option'
A>
B1
C<
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong tests to be selected.
Using '1' instead of '0' as threshold.
5fill in blank
hard

Fill all three blanks to create a test report dictionary filtering passed tests with duration over 1 second.

PyTest
report = {test['name']: test['duration'] for test in test_results if test['status'] == [1] and test['duration'] [2] [3]
Drag options to blanks, or click blank then click option'
A'passed'
B>
C1
D'failed'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'failed' instead of 'passed' for status.
Using '<' instead of '>' for duration comparison.
Using 0 or other numbers instead of 1 for duration.