Test Overview
This test suite demonstrates test independence by ensuring each test runs separately without relying on shared state. It verifies that modifying data in one test does not affect others.
Jump into concepts and practice - no test required
This test suite demonstrates test independence by ensuring each test runs separately without relying on shared state. It verifies that modifying data in one test does not affect others.
import pytest @pytest.fixture def sample_list(): return [1, 2, 3] def test_append_item(sample_list): sample_list.append(4) assert sample_list == [1, 2, 3, 4] def test_original_list_unchanged(sample_list): assert sample_list == [1, 2, 3]
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and collects tests | No tests executed yet | — | PASS |
| 2 | Runs test_append_item with fresh sample_list fixture | sample_list = [1, 2, 3] | Check sample_list after append is [1, 2, 3, 4] | PASS |
| 3 | Runs test_original_list_unchanged with fresh sample_list fixture | sample_list = [1, 2, 3] | Check sample_list is still [1, 2, 3] | PASS |
setup_method or setup_function.setup_method or setup_function -> Option Asetup_method/setup_function [OK]counter = 0
def test_first():
global counter
counter += 1
assert counter == 1
def test_second():
global counter
counter += 1
assert counter == 1shared_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