Verify independent test execution in pytest
Preconditions (2)
✅ Expected Result: Both tests pass independently with no shared state or order dependency
Jump into concepts and practice - no test required
import pytest # Simple calculator functions def add(a, b): return a + b def subtract(a, b): return a - b # Test functions def test_add(): result = add(2, 3) assert result == 5 def test_subtract(): result = subtract(5, 3) assert result == 2
This test script defines two simple functions: add and subtract. Each test function calls one of these and asserts the expected result.
Each test is independent: they do not share variables or state. This means you can run test_add or test_subtract alone or together in any order, and they will pass.
This avoids test interdependence, which is important because tests that depend on each other can cause confusing failures and make debugging hard.
Now add data-driven testing with 3 different input pairs for add and subtract functions
shared_list = []
def test_add_one():
shared_list.append(1)
assert shared_list == [1]
def test_add_two():
shared_list.append(2)
assert shared_list == [2]data = {}
def test_set_value():
data['key'] = 'value'
assert data['key'] == 'value'
def test_check_empty():
assert data == {}