avoid-duplicate-test-assertions

Path
lint/correctness/avoid-duplicate-test-assertions
Default
On (recommended)
Origin
Falcon
Domain
test
Rule
55 of 512
recommendedtest

Reports repeated identical, side-effect-free assertions in a straight-line block.

Only _test.dart files importing test or flutter_test are inspected. Calls must resolve to the test framework or matcher package. Comparisons use the actual and matcher source spelling. Literal/local actual values, standard matcher constants and simple framework matcher factories are eligible. Unknown local matcher objects and asynchronous local actuals are excluded. Intervening non-assertion statements, branches, effectful arguments, awaited calls, and chained calls end the comparison run.

Invalid

example.dartdart
import 'package:test/test.dart';
import 'package:test/test.dart' as t;
void main() {
  test('integer', () {
    expect(1, 1);
    expect(1, 1);
  });
  test('local variable', () {
    final value = true;
    expect(value, isTrue);
    expect(value, isTrue);
  });
  test('prefixed', () {
    t.expect(false, false);
    t.expect(false, false);
  });
  test('later', () {
    expectLater(1, 1);
    expectLater(1, 1);
  });
  test('nonadjacent pure assertions', () {
    expect(1, 1);
    expect(2, 2);
    expect(1, 1);
  });
  test('three copies', () {
    expect(null, null);
    expect(null, null);
    expect(null, null);
  });
}
void matcherExamples() {
  test('string matcher factory', () {
    expect('value', equals('value'));
    expect('value', equals('value'));
  });
}
This assertion is repeated; remove the duplicate assertion.
5 expect(1, 1);
6 expect(1, 1);
This assertion is repeated; remove the duplicate assertion.
10 expect(value, isTrue);
11 expect(value, isTrue);
This assertion is repeated; remove the duplicate assertion.
14 t.expect(false, false);
15 t.expect(false, false);
This assertion is repeated; remove the duplicate assertion.
18 expectLater(1, 1);
19 expectLater(1, 1);
This assertion is repeated; remove the duplicate assertion.
23 expect(2, 2);
24 expect(1, 1);
This assertion is repeated; remove the duplicate assertion.
27 expect(null, null);
28 expect(null, null);
This assertion is repeated; remove the duplicate assertion.
28 expect(null, null);
29 expect(null, null);
This assertion is repeated; remove the duplicate assertion.
34 expect('value', equals('value'));
35 expect('value', equals('value'));

Valid

example.dartdart
import 'package:test/test.dart';
void change() {}
int next() => 1;
class Checker { void expect(Object? a, Object? b) {} }
void main() {
  test('assignment resets', () {
    var value = 1;
    expect(value, 1);
    value = 2;
    expect(value, 1);
  });
  test('call resets', () { expect(1, 1); change(); expect(1, 1); });
  test('branches', () { if (next() == 1) { expect(1, 1); } else { expect(1, 1); } });
  test('first test', () { expect(1, 1); });
  test('second test', () { expect(1, 1); });
  test('different matcher', () { expect(1, 1); expect(1, 2); });
  test('effectful actual', () { expect(next(), 1); expect(next(), 1); });
  test('local shadow', () { void expect(Object? a, Object? b) {} expect(1, 1); expect(1, 1); });
  test('object member', () { final c = Checker(); c.expect(1, 1); c.expect(1, 1); });
  test('await boundary', () async { await expectLater(1, 1); await expectLater(1, 1); });
  test('different reason', () { expect(1, 1, reason: 'a'); expect(1, 1, reason: 'b'); });
}
void matcherBoundaries() {
  test('effectful matcher', () {
    expect(1, equals(next()));
    expect(1, equals(next()));
  });
  test('interpolated actual', () {
    expect('${next()}', 1);
    expect('${next()}', 1);
  });
  test('shadowed matcher factory', () {
    Object? equals(Object? value) => value;
    expect(1, equals(1));
    expect(1, equals(1));
  });
}

void asynchronousBoundaries(Object queue, Object eventMatcher) {
  test('stateful asynchronous matcher', () {
    expectLater(queue, eventMatcher);
    expectLater(queue, eventMatcher);
  });
  test('chained side effect breaks run', () {
    expectLater(1, 1);
    expectLater(1, 1).then((_) { next(); });
    expectLater(1, 1);
  });
}

Configure avoid-duplicate-test-assertions

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-duplicate-test-assertions": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/avoid-duplicate-test-assertions: <reason>