Skip to main content

format-test-name

added in: 1.0.0
⚙️
Pro+

Warns when a test or testWidgets name doesn't follow the configured pattern.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

Set test-name-pattern (default is none) to set name validation for test(...) invocations.

Set test-widgets-name-pattern (default is none) to set name validation for testWidgets(...) invocations.

analysis_options.yaml
dart_code_metrics:
rules:
- format-test-name:
test-name-pattern: '^good unit'
test-widgets-name-pattern: '^good widget'

Example

❌ Bad:

// LINT: This name does not match the configured pattern ^good unit'. Try changing it.
test('bad unit test', () {
final a = 1;
final b = 2;
final c = a + 1;
expect(b, c);
});

// LINT: This name does not match the configured pattern '^good widget'. Try changing it.
testWidgets('bad widget test', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
expect(find.text('Welcome'), findsOneWidget);
});

✅ Good:

test('good unit test', () {
final a = 1;
final b = 2;
final c = a + 1;
expect(b, c);
});

testWidgets('good widget test', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
expect(find.text('Welcome'), findsOneWidget);
});