Skip to content

duplicate_test_assertion

Reports an expect that repeats an earlier one of the same block with only assertions between. Check what you meant to check, or delete the repeat.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
delete the repeated assertion

An assertion that repeats an earlier one of the same block, with nothing but assertions between them. The second checks what the first already checked. The violation is on the repeated statement.

An assertion is a statement that calls a test framework’s expect by its bare name. Two assertions are the same when they are the same code, as no_equal_branches compares branches: the same tokens, every name meaning the same declaration.

Every assertion from the first to the second must only read values:

  • its value and each named argument such as reason: is a literal, a constant, a local variable, a parameter, a field or a top-level variable, or a chain of them. Read-only core members (length, isEmpty, first, keys…), an index into a core list or map and the core operators count too.
  • its matcher is such a value (isTrue, findsOneWidget), or a test framework matcher built from such values (equals(2), hasLength(n)).

It does not report:

  • a repeat after a statement that is not an assertion (counter.increment();), which can change what it reads;
  • a value that a call or a getter written in the source gives (iterator.moveNext(), stopwatch.elapsed), in the two assertions or in one between them: it can be another value the second time;
  • a matcher that runs code or waits: throwsA, throwsStateError, emits…, completion, completes, prints and their kin;
  • expectLater, which waits;
  • two assertions in different blocks;
  • a method named expect, or an expect that is not a test framework’s.

A repeated assertion is usually a line you copied to check the next thing and did not change. The property you meant to check is not checked, but the test reads as if it were. The rule is advisory: the test still checks the first time.

Violating
final cart = Cart()..add(item);
expect(cart.count, 1);
expect(cart.total, 9);
expect(cart.count, 1);
Corrected
final cart = Cart()..add(item);
expect(cart.count, 1);
expect(cart.total, 9);
expect(cart.isEmpty, isFalse);

The fix deletes the repeated statement and its line. It is not offered when a comment stands directly before the statement, because the deletion would take the comment with it.

The rule has only the two common rule parameters, 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.
assay:
rules:
duplicate_test_assertion:
exclude: ["test/fixtures/**"]

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

// The second check runs after the listener has fired, which the test counts.
// ignore: duplicate_test_assertion
expect(model.value, 1);

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