0
0
PyTesttesting~10 mins

Test independence 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.[1]
def test_example():
    assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Amark.dependency
Bmark.parametrize
Cmark.skip
Dmark.independent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.mark.dependency instead of @pytest.mark.independent
Confusing skip or parametrize markers with independence
2fill in blank
medium

Complete 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'
Afunction
Bsession
Cmodule
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'session' scope which shares setup across all tests
Using 'module' or 'class' scope which groups tests
3fill in blank
hard

Fix 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'
ANone
B0
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Assuming the list is empty in test_append
Not resetting shared state between tests
4fill in blank
hard

Fill 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'
Aempty_list
Bshared_list
Ctemp_list
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for fixture and argument
Using a shared list causing test dependency
5fill in blank
hard

Fill 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'
Aresource_fixture
Bresource
Dres
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent names causing errors
Not resetting shared state in fixture teardown