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.[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 @pytest.mark.dependency instead of @pytest.mark.independent
Confusing skip or parametrize markers with independence
✗ Incorrect
Using @pytest.mark.independent marks the test as independent, ensuring it does not rely on other tests.
2fill in blank
mediumComplete the code to ensure each test runs with a fresh setup using fixtures.
PyTest
import pytest @pytest.fixture(autouse=True, scope='function') def setup(): # Setup code here yield # Teardown code here def test_one(): assert True def test_two(): assert True # Each test uses [1] fixture to stay independent.
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'session' scope which shares setup across all tests
Using 'module' or 'class' scope which groups tests
✗ Incorrect
Using a 'function' scoped fixture runs setup and teardown for each test function, keeping tests independent.
3fill in blank
hardFix the error in the test that shares state causing dependency.
PyTest
shared_list = [] def test_append(): shared_list.append(1) assert len(shared_list) == [1] def test_clear(): shared_list.clear() assert len(shared_list) == 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assuming the list is empty in test_append
Not resetting shared state between tests
✗ Incorrect
The first test appends one item, so the length should be 1. Sharing state causes dependency, so tests should avoid this.
4fill in blank
hardFill both blanks to create independent tests using fixtures.
PyTest
import pytest @pytest.fixture def [1](): return [] def test_add_item([2]): [2].append('item') assert len([2]) == 1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for fixture and argument
Using a shared list causing test dependency
✗ Incorrect
Using the fixture 'empty_list' as argument ensures each test gets a fresh list, keeping tests independent.
5fill in blank
hardFill all three blanks to write independent tests with setup and teardown.
PyTest
import pytest @pytest.fixture def [1](): resource = {'count': 0} yield resource resource['count'] = 0 def test_increment([2]): [2]['count'] += 1 assert [2]['count'] == 1 def test_reset([3]): assert [3]['count'] == 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent names causing errors
Not resetting shared state in fixture teardown
✗ Incorrect
Using the fixture 'resource' ensures each test gets a fresh resource dict, keeping tests independent.