Unit tests
Small tests for rules and calculations. They test behaviour through public functions, run in milliseconds, and give the same result every time.
- Recommended 10
Recommended
Test behaviour through the public interface
TST-4A unit test calls a function or class the way callers do, and checks what it returns or changes.
Fake the clock and the network in unit tests
TST-5Unit tests never depend on the clock, random numbers or the network. They pass on any day, on any machine.
Test the edges and the errors
TST-6For every rule with a limit, test the values on both sides of it. For every input that can be rejected, test the rejection.
Arrange, act and assert
TST-7Write each test in three parts. Set up the data, do one action, then check the result.
Give each test one reason to fail
TST-8Each test checks one behaviour. When it fails, its name tells you what broke.
Keep unit tests fast
TST-9Each unit test runs in milliseconds, and the whole unit suite runs in seconds.
Check rules that hold for any input with property tests
TST-10When a rule must hold for every input, such as a round trip or a total, state it once as a property. Let a tool generate hundreds of inputs to test it.
Test rules with several conditions as a table
TST-11When a result depends on two or more conditions, list every combination in a table with its expected result. Run each row as a test.
Test sequences of actions against a simple model
TST-49For objects that change over many steps, such as a cart, a balance or a booking calendar, let a tool generate sequences of actions. After each step, compare the object with a simple model.
Test times at the daylight saving changes
TST-50Every feature that schedules, repeats or bills by local time has tests on both clock changes of a year. One test covers the hour that is skipped, and one covers the hour that happens twice.
Read this with
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/unit-tests">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.