0
0
PyTesttesting~10 mins

Avoiding test interdependence 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 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'
Aisolated
Bindependent
Cindep
Dindep_test
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent marker name.
Using abbreviations instead of full marker names.
2fill in blank
medium

Complete 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'
A''
BNone
CFalse
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Resetting to None causes type errors.
Using empty string instead of zero.
3fill in blank
hard

Fix 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'
A1
B0
C2
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Assuming the list has previous items from other tests.
Using zero or None as length after append.
4fill in blank
hard

Fill 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'
Afresh_list
B[]
C{}
Dlist()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a dictionary instead of a list.
Using a shared list literal that causes test interference.
5fill in blank
hard

Fill 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'
Aone
B1
Ctwo
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same test name for both tests.
Expecting wrong list length after append.