Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a fixture that sets up a shared resource.
PyTest
import pytest @pytest.fixture def shared_resource(): resource = {'count': 0} yield resource resource.clear() def test_increment(shared_resource): shared_resource['count'] [1] 1 assert shared_resource['count'] == 1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' decreases the count, causing the test to fail.
Using '*=' or '/=' changes the count incorrectly.
✗ Incorrect
The '+=' operator increments the count by 1, which matches the assertion.
2fill in blank
mediumComplete the code to ensure the shared resource is reset before each test.
PyTest
import pytest @pytest.fixture(autouse=True) def reset_resource(shared_resource): shared_resource['count'] = [1] def test_reset(shared_resource): assert shared_resource['count'] == 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting count to 1 causes the test to fail because the assertion expects 0.
Setting count to None or negative values is not appropriate here.
✗ Incorrect
Resetting the count to 0 ensures the shared resource starts fresh for each test.
3fill in blank
hardFix the error in the test that modifies the shared resource incorrectly.
PyTest
def test_decrement(shared_resource): shared_resource['count'] [1] 1 assert shared_resource['count'] == -1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+=' increases the count, causing the assertion to fail.
Using '*=' or '/=' changes the value incorrectly.
✗ Incorrect
Using '-=' decreases the count by 1, matching the expected assertion of -1.
4fill in blank
hardFill both blanks to create a fixture that yields a resource and cleans it after use.
PyTest
import pytest @pytest.fixture def resource(): data = [] yield [1] [2].clear()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Yielding or clearing a variable not defined causes errors.
Using different variable names for yield and clear breaks the cleanup.
✗ Incorrect
The fixture yields 'data' and then clears 'data' after the test finishes.
5fill in blank
hardFill all three blanks to create a test that uses a shared resource and asserts its state.
PyTest
def test_shared_state([1]): [2]['count'] += 2 assert [3]['count'] == 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names causes NameError or test failures.
Referencing undefined variables breaks the test.
✗ Incorrect
The test function parameter and all references use 'shared_resource' to access the shared data.