Skip to content

format_test_name

v1.0.0WarningConfigurableTesting Rules

Holds every test(...) and testWidgets(...) description to a regular expression you choose.

A test name is read without the code beside it — in CI output, in a failure report, in a bisect log — so it has to carry the expectation on its own.

This rule is in no preset and reports nothing until you set pattern:. There is no defensible default house style, so every example below shows the configuration that produces it.

analysis_options.yaml
many_lints:
rules:
format_test_name:
pattern: 'should .*'

pattern must match the whole description, not merely appear in it.

pattern: 'should .*'
// Don't
test('caches the user', () {});
test('Should load a user', () {}); // capital S is not `should`
// Do
test('should cache the user', () {});
test('should load a user', () {});
pattern: 'given .+ when .+ then .+'
// Don't
test('offline load fails', () {});
// Do
test('given no network when loading then it throws', () {});

pattern is a plain Dart regular expression, so alternation works:

pattern: '(should|must) .*'
// Don't
test('returns the cached user', () {});
// Do
test('should return the cached user', () {});
test('must reject an expired token', () {});

A group names a subject, not an expectation, so it is not held to the pattern unless you ask:

pattern: 'should .*'
check_groups: false # the default
group('UserRepository', () {
test('should return the cached user', () {});
});
pattern: '(should|when|given) .*'
check_groups: true
// Don't
group('UserRepository', () { // now reported
test('should return the cached user', () {});
});
// Do
group('when the cache is warm', () {
test('should return the cached user', () {});
});

exclude takes globs, so a legacy suite can keep its old names while new code is held to the convention:

analysis_options.yaml
many_lints:
rules:
format_test_name:
pattern: 'should .*'
exclude:
- 'test/legacy/**'
Option Type Default Description
pattern string — A regular expression the whole description must match. Unset means the rule is silent
check_groups bool false Also hold group(...) names to the pattern

Only string literals are checked. An interpolated or computed description — test('should load $name', ...), test(kCacheCase, ...) — is never reported, because it cannot be read without evaluating it. That keeps parameterised tests silent.

Only test, testWidgets and (opt-in) group. A description passed to a custom helper of your own is not checked.

An invalid pattern is ignored, not reported. A plugin cannot report against a YAML file, so a pattern that is not valid regex leaves the rule silent rather than failing loudly.

many_lints.yaml
rules:
format_test_name: false

To keep the rule on but skip certain paths, use per-rule exclude.