Skip to content

prefer_correct_test_file_name

v1.0.0WarningConfigurableTesting Rules

This rule flags a file under test/ that declares tests but is not named *_test.dart.

This rule is in the recommended preset.

package:test only runs files matching *_test.dart. A file named user_repository_tests.dart or test_user_repository.dart still compiles, still passes analysis, and is silently never executed — the suite stays green by not running the test at all. Nothing else catches this: the analyzer is happy and CI reports success.

// test/user_repository_tests.dart — plural, so the runner never picks it up
void main() {
test('should load a user', () {});
}
// test/test_user_repository.dart — prefix instead of suffix, same result
void main() {
test('should load a user', () {});
}
test/user_repository_test.dart
void main() {
test('should load a user', () {});
}

Files that declare no tests are never reported

Section titled “Files that declare no tests are never reported”

The rule reports only a file whose top-level main contains a test(...), testWidgets(...) or group(...) call. Helpers, fixtures, builders and robots live under test/ without being test files, and are left alone:

// test/support/user_builder.dart — no tests declared, never reported
class UserBuilder {
UserBuilder withName(String name) => this;
}

By default the rule looks at test/, integration_test/ and test_driver/. A project with a suite elsewhere adds it:

analysis_options.yaml
many_lints:
rules:
prefer_correct_test_file_name:
additional_directories: [e2e]
// e2e/checkout_flow.dart — now reported; rename to checkout_flow_test.dart
void main() {
test('completes a checkout', () {});
}

Use directories: instead to replace the defaults outright rather than add to them.

See also: package:test, Flutter: testing

analysis_options.yaml
many_lints:
rules:
prefer_correct_test_file_name:
directories: [test, integration_test]
Option Type Default Description
directories list of strings [test, integration_test, test_driver] Top-level directories the rule applies to
additional_directories list of strings [] Directories to add to the defaults, instead of restating them

To disable this rule:

many_lints.yaml
rules:
prefer_correct_test_file_name: false

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