Skip to content

duplicate_test_name

Reports a test whose full name is the name of an earlier test beside it. Give the second test a name that says what it checks.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
none

A test whose full name (the names of its groups and its own) is the name of an earlier test beside it. The violation is on the name, the first argument, of the test that repeats.

  • Tests and groups are found as for test_file_not_collected: a call of one of the test_functions or group_functions that resolves to a test framework or to the test side.
  • Two tests are beside each other when both are statements of their own in the same block: the body of a group, the body of main, or any other block.
  • Groups of one name beside each other count as one group, since the runner names their tests alike.
  • test, testWidgets and blocTest declare tests alike.
  • Only names that are string literals without interpolation are compared.

It does not report:

  • the same name in two groups of different names;
  • two groups of one name whose tests have names of their own: a file often splits one subject into several groups, each with its own setUp;
  • a test and a group of one name;
  • two tests of one name in the two branches of an if, in a loop or in a closure, where only one, or each in turn, runs;
  • a computed name ('$name works');
  • a method or a local function named like a test function, or a call outside the test side.

A runner reports a test by the names of its groups and its own. With two tests of one name in one group:

  • the report prints the same line twice;
  • a failure names both;
  • --name, --plain-name and an editor’s run button pick both, or the wrong one.

The second test is usually a copy whose body you changed but not its name, so the name no longer says what it checks. The rule is advisory: both tests run.

Violating
group('cart', () {
test('adds an item', () => expect(Cart().add(1).count, 1));
test('adds an item', () => expect(Cart().add(1).add(1).count, 2));
});
Corrected
group('cart', () {
test('adds an item', () => expect(Cart().add(1).count, 1));
test('adds a second item', () => expect(Cart().add(1).add(1).count, 2));
});

There is no fix: only you know what the second test checks.

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.
test_functions [test, testWidgets, blocTest] The functions whose call declares a test, matched by name as written.
group_functions [group] The functions whose call declares a group of tests, matched by name as written.
assay:
rules:
duplicate_test_name:
exclude: ["test/fixtures/**"]
test_functions: [test, testWidgets, blocTest, patrolTest]

Write why on the line directly above the suppression, which goes above the repeated name:

// The two cases differ by the set-up, which names them in the report.
// ignore: duplicate_test_name
test('loads', () async {

// ignore_for_file: assay/duplicate_test_name silences it in a whole file, and // ignore: assay/duplicate_test_name with the prefix works too. See Suppressions.