---
title: "Arrange, act and assert · Agent Wranglers"
url: https://agentwranglers.se/playbook/testing/unit-tests/arrange-act-assert
updated: 2026-10-20
licence: "All rights reserved"
---

# Arrange, act and assert

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Recommended
- Chapter: [Unit tests](https://agentwranglers.se/playbook/testing/unit-tests)
- Rule: Write each test in three parts. Set up the data, do one action, then check the result.
- How: Separate the three parts with a blank line. Do only one action per test. Keep the setup short by using helpers or factories. If a test needs many actions, it is probably a flow that belongs in an integration test.
- Check: Pick ten tests at random. Each has setup, then one call to the code under test, then only assertions. No call to the code under test follows an assertion.

Example:

```python
def test_cancelled_order_releases_stock():
    stock = Stock(sku="MUG-1", count=3)
    order = place_order(stock, quantity=2)

    order.cancel()

    assert stock.count == 3
```

```ts
test('a cancelled order releases its stock', () => {
  const stock = new Stock('MUG-1', 3);
  const order = placeOrder(stock, 2);

  order.cancel();

  expect(stock.count).toBe(3);
});
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers testing playbook: https://agentwranglers.se/playbook/testing/unit-tests/arrange-act-assert`.

- Previous: [Test the edges and the errors](https://agentwranglers.se/playbook/testing/unit-tests/test-the-edges)
- Next: [Give each test one reason to fail](https://agentwranglers.se/playbook/testing/unit-tests/one-reason-to-fail)
- [Unit tests](https://agentwranglers.se/playbook/testing/unit-tests)
