Unit tests Recommended
Arrange, act and assert
Write each test in three parts. Set up the data, do one action, then check the result.
How to do it
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.
Example
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
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);
});
How to 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.
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.