Complete worked example
python
import pytest
@pytest.fixture
def basket():
return ["apple"]
def test_initial_item(basket):
assert basket == ["apple"]
def test_append_item(basket):
basket.append("pear")
assert basket == ["apple", "pear"]- Reasoning
- The worked fixture returns a new list containing one item. One test observes its initial contents; another appends and checks the resulting contents. Because the fixture is function-scoped, both start with the same independent setup, not with the other test’s modified list. Fixture dependencies can be explicit arguments of other fixtures too. Keep the graph understandable: do not create a long chain of fixtures that only rename the same constant. A simple literal in one test may be clearer than a fixture used once with no meaningful setup responsibility.