Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect marker names like 'serial_run' or 'run_serial'.
Trying to use 'order' which is unrelated.
✗ Incorrect
The correct marker to indicate a test should run serially is '@pytest.mark.serial'.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of integers for order.
Starting order from 0 instead of 1.
✗ Incorrect
The '@pytest.mark.order(1)' decorator sets the test to run first in order.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parallel' or 'concurrent' which allow parallel runs.
Using 'async' which is unrelated to test ordering.
✗ Incorrect
Marking the test as 'serial' ensures it does not run in parallel, avoiding conflicts.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong tests to be selected.
Using '1' instead of '0' as threshold.
✗ Incorrect
The comprehension keeps tests with priority greater than 0, skipping low priority tests.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The dictionary includes tests that passed and took longer than 1 second.