Bird
Raised Fist0
PyTesttesting~10 mins

Test independence in PyTest - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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

Practice

(1/5)
1. Why is test independence important in pytest?
easy
A. It groups tests to run in a specific order.
B. It allows tests to share variables for faster execution.
C. It ensures tests do not affect each other and run reliably alone.
D. It makes tests run only when previous tests pass.

Solution

  1. Step 1: Understand test independence concept

    Test independence means each test runs alone without relying on others.
  2. Step 2: Identify why independence matters

    This prevents tests from failing due to side effects or order, making results reliable.
  3. Final Answer:

    It ensures tests do not affect each other and run reliably alone. -> Option C
  4. Quick Check:

    Test independence = tests run alone [OK]
Hint: Tests should run alone without relying on others [OK]
Common Mistakes:
  • Thinking tests must share data to be efficient
  • Believing tests run only if previous tests pass
  • Assuming test order controls correctness
2. Which pytest feature helps keep tests independent by running setup code before each test?
easy
A. Using setup_method or setup_function
B. Using yield_fixture to share data
C. Using @pytest.mark.parametrize
D. Using pytest.skip() to skip tests

Solution

  1. Step 1: Identify setup features in pytest

    Pytest runs setup code before each test using setup_method or setup_function.
  2. Step 2: Understand their role in test independence

    Setup prepares fresh state for each test, avoiding shared state and keeping tests independent.
  3. Final Answer:

    Using setup_method or setup_function -> Option A
  4. Quick Check:

    Setup before each test = setup_method/setup_function [OK]
Hint: Setup code before each test keeps tests independent [OK]
Common Mistakes:
  • Confusing parameterize with setup
  • Using yield_fixture to share state incorrectly
  • Skipping tests does not setup state
3. Given the code below, what will be the output when running both tests?
counter = 0

def test_first():
    global counter
    counter += 1
    assert counter == 1

def test_second():
    global counter
    counter += 1
    assert counter == 1
medium
A. First test passes, second test fails
B. Both tests pass
C. First test fails, second test passes
D. Both tests fail

Solution

  1. Step 1: Analyze test_first behavior

    Initially, counter=0. test_first increments to 1 and asserts counter == 1, so it passes.
  2. Step 2: Analyze test_second behavior

    counter is now 1 from previous test. test_second increments to 2 and asserts counter == 1, which fails.
  3. Final Answer:

    First test passes, second test fails -> Option A
  4. Quick Check:

    Shared state causes second test failure [OK]
Hint: Shared global state breaks test independence [OK]
Common Mistakes:
  • Assuming counter resets automatically
  • Thinking both tests run with fresh state
  • Ignoring global variable effects
4. Identify the problem in this pytest code that breaks test independence:
shared_list = []

def test_add_one():
    shared_list.append(1)
    assert len(shared_list) == 1

def test_add_two():
    shared_list.append(2)
    assert len(shared_list) == 1
medium
A. shared_list should be a global variable inside tests
B. Assertions are incorrect; they should check for length 2
C. Tests run in parallel causing race conditions
D. shared_list is not cleared between tests causing length to grow

Solution

  1. Step 1: Understand shared_list usage

    shared_list is defined outside tests and not reset, so it keeps growing with each test.
  2. Step 2: Identify why independence breaks

    Because shared_list is not cleared, test_add_two sees length 2, but asserts length 1, causing failure.
  3. Final Answer:

    shared_list is not cleared between tests causing length to grow -> Option D
  4. Quick Check:

    Shared mutable state without reset breaks independence [OK]
Hint: Reset shared data between tests to keep independence [OK]
Common Mistakes:
  • Assuming pytest resets global variables automatically
  • Changing assertions instead of fixing state
  • Confusing parallel runs with shared state issues
5. You want to ensure two pytest tests that modify a database table run independently. Which approach best maintains test independence?
hard
A. Run tests in a fixed order so changes apply sequentially
B. Use a fixture to create and rollback a transaction for each test
C. Share a global database connection and commit changes after all tests
D. Skip tests that depend on database state

Solution

  1. Step 1: Understand database state isolation

    To keep tests independent, each test should not affect others' database state.
  2. Step 2: Choose best isolation method

    Using a fixture that creates a transaction and rolls back after each test resets database state, ensuring independence.
  3. Final Answer:

    Use a fixture to create and rollback a transaction for each test -> Option B
  4. Quick Check:

    Transaction rollback per test = independence [OK]
Hint: Rollback DB changes after each test to isolate state [OK]
Common Mistakes:
  • Relying on test order for correctness
  • Sharing global DB connection without isolation
  • Skipping tests instead of fixing independence