Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark the test as independent using pytest.
PyTest
import pytest @pytest.mark.[1] def test_example(): assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent marker name.
Using abbreviations instead of full marker names.
✗ Incorrect
The correct pytest marker to indicate test independence is independent.
2fill in blank
mediumComplete the code to reset the shared resource before each test using a fixture.
PyTest
import pytest @pytest.fixture(autouse=True) def reset_resource(): global resource resource = [1] resource = 0 def test_increment(): global resource resource += 1 assert resource == 1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Resetting to None causes type errors.
Using empty string instead of zero.
✗ Incorrect
Resetting the shared resource to 0 ensures tests start fresh.
3fill in blank
hardFix the error in the test to avoid dependence on previous test results.
PyTest
def test_append_item(): items = [] items.append('apple') assert len(items) == [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assuming the list has previous items from other tests.
Using zero or None as length after append.
✗ Incorrect
After appending one item, the list length should be 1.
4fill in blank
hardFill both blanks to create a fixture that provides a fresh list for each test.
PyTest
import pytest @pytest.fixture def [1](): return [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a dictionary instead of a list.
Using a shared list literal that causes test interference.
✗ Incorrect
The fixture named fresh_list returns a new empty list using list() each time.
5fill in blank
hardFill all three blanks to write two independent tests using the fresh_list fixture.
PyTest
def test_add_item_[1](fresh_list): fresh_list.append('item') assert len(fresh_list) == [2] def test_clear_list_[3](fresh_list): fresh_list.clear() assert len(fresh_list) == 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same test name for both tests.
Expecting wrong list length after append.
✗ Incorrect
The first test is named test_add_item_one and expects length 1. The second test is named test_clear_list_two.