🧪Test Fixture
Also known as Fixture, Fixtures, Test Setup, pytest Fixture
The setup a test starts from. It might be a sample user or a cart.
Think of it like
Think of it like setting the table for dinner. Each guest gets the same plate, fork and cup.
Example
Emma's fixture makes a cart with one mug. Ten tests use it, so none of them builds a cart by hand.
Why it matters
Tests that start from the same state give the same answer every time.
Definition
Example code
python
import pytest
@pytest.fixture
def cart():
return Cart(items=["mug"])
def test_total(cart):
assert cart.total() == 8Common mistake
Key takeaways
- 1Known start. Every test begins the same way.
- 2Write once. Many tests share one setup.
- 3Clean up after. Leave nothing for the next test.