Name tests for the behavior and the condition
LOW1.0.0
When to apply Before writing, changing, or reviewing test names or descriptions in any test framework.
Name each test for the result it expects and the condition that produces it, such as "should return an empty list when the filter matches nothing".
Implementation
- Include the expected behavior and the situation, in the project's chosen form, such as
it('should ... when ...')orTestParseDate_RejectsInvalidMonth. - Name the observable result, not the function or implementation step being exercised.
- Enforce the chosen form with a lint rule when the test framework's plugin supports one, such as Vitest's
valid-titlewithmustMatch.
This is a naming convention; the exact form is a project choice, but each test should state behavior and condition.
Rationale
A failing test's name is often the first thing a developer reads. A name that states the behavior and condition says what broke without opening the test, and makes gaps visible when reading the list of tests.
Background: Testing philosophy.
Examples
Incorrect (counterexample):
it('parseDate works');
it('after title is confirmed user description is rendered');
Correct:
it('should return the date formatted as YYYY-MM when the input is an ISO date');
it('should render the user description when the title is confirmed');
Validation
Read the test names in a file and check that each states an expected result and the condition that produces it.
A different consistent form, such as Go's TestXxx_Condition names, is not a violation.