ENG-5Recommended
Unit test every branch and calculation
Give every source file that holds branches or calculations a unit test file. The tests run on their own, without a network, a database or a browser.
How to do it
Keep small, fast tests next to the code for parsing, prices, dates, permissions and any other if or arithmetic. Use the language's usual runner, such as node --test, pytest or PHPUnit. Run them before every build. Set the coverage minimum as Treat coverage as a floor says, and see the testing playbook for what a good unit test checks. Each test must also pass on any day and any machine, as Fake the clock and the network in unit tests says.
Examples
JavaScript example
// date.test.js, run with: node --test
import test from 'node:test';
import assert from 'node:assert/strict';
import { formatDate } from './date.js';
test('formats a date for people', () => {
assert.equal(formatDate('2026-10-20'), '20 October 2026');
});
Python example
# test_dates.py, run with: pytest
from dates import format_date
def test_formats_a_date_for_people():
assert format_date("2026-10-20") == "20 October 2026"
How to check
A script lists every source file in the logic folders, such as src/domain, that has no matching test file. The list is empty. The test run fails when branch coverage drops below the set minimum. The unit tests finish within the unit budget of Keep the suite within a time budget.
Sources
Related rules
- TST-32 Treat coverage as a floor in Testing
- TST-4 Test behaviour through the public interface in Testing
- TST-5 Fake the clock and the network in unit tests in Testing
- TST-30 Keep the suite within a time budget in Testing
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/engineering/build-test-and-deploy/unit-tests-for-the-tricky-parts">Agent Wranglers engineering playbook</a>. In code you write, add a comment next to it with the same link.