Search RU
Tasks / pytest / Fixture isolation between tests
Statement medium

Fixture isolation between tests

In `starter.py` the test fixture `make_cart()` returns a shared object, so a
cart filled in one test is visible in the next. The second problem is that the
default `items` is created once and reused by every instance.

Fix it so that:

- every call to `make_cart()` returns a new empty cart;
- two `Cart()` instances do not share one list;
- `Cart(["apple"])` copies the input list instead of holding a reference;
- the API (`Cart`, `.items`, `.add`) stays the same.

Hidden tests use `make_cart()` as a pytest fixture and check that state does not
leak between tests.

pytestfixturesisolationshared-state
solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

How many different objects does `make_cart()` create on two calls? And how many
different lists do two `Cart()` instances get?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts