# Test Fixture

> The setup a test starts from. It might be a sample user or a cart.

**Field:** Software Development  
**Category:** Testing & Quality  
**Also known as:** Fixture, Fixtures, Test Setup, pytest Fixture

## Think of it like

Think of it like setting the table for dinner. Each guest gets the same plate, fork and cup.

## Definition

A test fixture is the setup a test needs before it runs. It might be a sample user, a cart with items or an empty database. Many tests share one fixture, so the setup is written once. After the test, the fixture can clean up what it made. Every test then starts from the same known state.

## Example

Emma's fixture makes a cart with one mug. Ten tests use it, so none of them builds a cart by hand.

## Example code

```python
import pytest

@pytest.fixture
def cart():
    return Cart(items=["mug"])

def test_total(cart):
    assert cart.total() == 8
```

The fixture builds the cart once, and the test asks for it by name.

## Why it matters

Tests that start from the same state give the same answer every time.

## Common mistake

Sharing one fixture that tests change. One test's leftovers then break the next test.

## Three principles

- **Known start.** Every test begins the same way.
- **Write once.** Many tests share one setup.
- **Clean up after.** Leave nothing for the next test.

## Related terms

- [Test Case](https://learn.benibix.com/term/development/test-case)
- [Arrange-Act-Assert (AAA)](https://learn.benibix.com/term/development/arrange-act-assert)
- [pytest](https://learn.benibix.com/term/development/pytest)
- [Seed Data](https://learn.benibix.com/term/development/seed-data)
- [Flaky Test](https://learn.benibix.com/term/development/flaky-test)

![One test drawn as a row of steps. First a fixture box sets up a cart holding a mug. Then three lit-up steps: arrange, set up the inputs; act, run the one thing being tested; assert, check the result. Last, teardown cleans up. Below, two paths branch from the same feature: the happy path, where a valid card pays, and edge cases, like an empty cart or an expired card.](https://learn.benibix.com/diagrams/dev3-test-anatomy.svg)

---

Source: https://learn.benibix.com/term/development/test-fixture
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
