Skip to content

test_requires_assertion

Reports a test whose body cannot reach any assertion. Assert what the test’s name promises.

Tier
blocking
Scope
Dart
Fix
none

A test whose body can reach no assertion. The violation covers the test function’s name: the test of test('adds two numbers', () { … }).

These are violations:

  • a test with an empty body: test('does nothing', () {});
  • a test that only runs the code under test: test('loads', () async { await load(); });
  • a widget test that only pumps a widget;
  • a test that only sets up a mock and calls the code under test.

The rule is blocking, so it reports only when it is sure. A call of expect*, verify*, assert*, check* or fail counts as an assertion, anywhere in the body. So does anything the rule cannot read and that could hold one, such as a helper from another test file. It does not report:

  • a test whose body holds an assertion anywhere, even one not reached on every path;
  • a test that asserts only inside a helper its own file declares;
  • a test that calls a helper of another file of the test side.

A test without an assertion checks only that nothing in it throws: it shows green and counts towards coverage even if the code returns the wrong value. AI coding agents write such tests when asked for tests without working out what the code should return, so the test claims a check it does not make. no_trivial_assertion is the other half: it reports an assertion that can never fail, such as expect(true, isTrue).

Violating
test('parses an amount with a currency', () {
final amount = Amount.parse('12.50 EUR');
print(amount);
});
testWidgets('shows the total', (tester) async {
await tester.pumpWidget(const CartPage());
});
Correctedby asserting what the name of the test promises
test('parses an amount with a currency', () {
final amount = Amount.parse('12.50 EUR');
expect(amount.cents, 1250);
expect(amount.currency, 'EUR');
});
testWidgets('shows the total', (tester) async {
await tester.pumpWidget(const CartPage());
expect(find.text('Total: 0.00 EUR'), findsOneWidget);
});

A test that really only checks that nothing throws says so with an assertion: expect(() => parse(text), returnsNormally), or in a widget test expect(tester.takeException(), isNull).

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

Rule parameter Default Effect
test_functions [test, testWidgets] The functions whose call declares a test. Your list replaces the default.
assertion_patterns [expect*, verify*, assert*, check*, fail] The names whose call counts as an assertion. A * stands for any run of characters, or none. Your list replaces the default.
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.
assay:
rules:
test_requires_assertion:
test_functions: [test, testWidgets, appTest]
assertion_patterns: ["expect*", "verify*", "assert*", "check*", fail, "*Matches"]

List a test wrapper that your test directory declares, such as appTest above, in test_functions: its calls are then judged as tests (see Test wrappers).

Write why on the line directly above the line where the test call starts:

// The page only has to build here; the golden test asserts on how it looks.
// ignore: assay/test_requires_assertion
testWidgets('builds the cart page', (tester) async {
await tester.pumpWidget(const CartPage());
});

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

All cases

Besides the three cases above, it does not report:

  • a test that calls a function value it cannot read (see below);
  • a call of a test function with no function literal among its arguments: test('by its name', body);
  • a method of the same name called on an object: probe.test('a method', () {});
  • a function literal handed to any other call: group, setUp, tearDown.

The rule reads the test’s body and the helpers of the same file. Whatever it cannot read and could hold an assertion counts as one.

What a test is. A call of a test function by its bare name, with at least one function literal among its arguments. The test functions are test and testWidgets, unless test_functions says otherwise. The function literals are the test’s body.

The name must resolve to a top-level function or variable of a library that is a test framework’s or on the test side (see below): the test of package:test, the testWidgets of package:flutter_test, or a wrapper your test directory declares and you list in test_functions. A method of the enclosing class, a parameter or a local function merely named test is not one, nor is a test function of code unrelated to tests.

What counts as an assertion. Any of these, anywhere in the body, at any depth (a loop, a callback, a local function):

  • A call whose name matches an assertion pattern. The patterns are expect*, verify*, assert*, check* and fail, unless assertion_patterns says otherwise. The name is the one right before the argument list: expect(…), expectLater(…), robot.expectVisible(), verifyNever(…) and check(total).equals(3) all match the defaults. An assert statement counts as a call of assert. Leading underscores are ignored, so _expectSilent(…) and robot._verifyTapped() match; a pattern that itself starts with an underscore still works.

  • A helper of the same file that asserts, whatever its name: totalsAreRight(cart) counts when the file declares totalsAreRight and its body calls expect. A helper is a function (top-level or local), a method, or a variable with an initializer. It asserts when its own body does, by anything in this list, through any number of further helpers of the file. Helpers that call each other in a circle are followed once. A helper handed on without a call counts too: values.forEach(isValid).

  • A helper from another file of the test side, whatever it is called or does: a function or a method (an extension method, or the call of a called object), called or handed on. The rule does not read other files, and no name tells what a project’s helper does: shouldPass(value, matcher), harness.run(steps), tester.pumpApp(page).

  • A function value the file cannot read, called or handed on: a parameter, variable, field or getter of a function type. The file can read it in two cases, and then reads its value in its place:

    • it is a parameter of the very function being read (a helper’s parameter gets its value from the calling code, which is read too);
    • the file gives it one fixed value: a final or const variable, or a local variable never assigned again, with an initializer; or a getter with a body.

    Every other one counts: the body a test wrapper is given and calls, a late variable that setUp assigns, a field a constructor sets, an entry of a table of cases, a loop variable, the parameter of a callback. So do a function taken from a collection by index, steps[0](), and a call on a name of type dynamic.

These do not count, so a test that asserts only so is reported:

  • an assertion by hand, if (total != 3) throw StateError('…');: call fail('…') or expect instead;
  • an assertion only inside a constructor, Fixture(), or inside a getter, setter or operator of another file of the test side;
  • a framework’s assertion handed on without a call, messages.forEach(fail);
  • a named argument that is not a call, such as expect: () => [...]: with blocTest in test_functions, such a test is reported unless another argument, such as verify:, calls an assertion.

A skipped test is reported like any other.

The test side. The rule sees only the resolved code, so it tells where a declaration lives from its library’s address:

  • A library in a directory named test, integration_test or test_driver is on the test side. A library under a package’s lib has a package: address (package:shop/cart.dart) and lies in no test directory. Any other library counts when a directory of its file’s address has such a name.
  • A library that can see a test framework is on the test side wherever it lies: it imports one, directly or through an export. That is how a package of test helpers, or a project’s lib/testing.dart, is known.
  • The test frameworks themselves are not: test, test_api, test_core, matcher, checks, flutter_test, integration_test, flutter_driver, mockito and mocktail. So tester.pumpWidget(…), find.byType(…) and when(…) are judged by their names alone, and are not assertions.
  • Generated mocks, *.mocks.dart, are not: a mock’s methods record that they were called and assert nothing.
  • The SDK, the code under test and every other package are not. A test that only calls them is what the rule is for.

Test wrappers are handled from both sides:

void appTest(String description, Future<void> Function(WidgetTester) body) {
testWidgets(description, (tester) async {
await tester.pumpWidget(const App());
await body(tester);
});
}

The testWidgets inside the wrapper is not reported: it calls body, which it was given. With appTest in test_functions, a call appTest('adds', (tester) async { … }) is a test like any other, and is reported when its body asserts nothing.

  • A test function is matched by its whole name, as written, with no wildcard.
  • An assertion pattern is matched against a call’s whole name, case-sensitively: expect* matches expect, expectLater and expectVisible, not unexpected or Expect. fail matches only fail, not failFast. A * may stand anywhere, more than once: *Matches, *golden*. An empty entry is ignored.
  • Quote a pattern that begins with * in YAML, as above.
  • With an empty test_functions, nothing is a test and the rule reports nothing. With an empty assertion_patterns, nothing is an assertion and every test is reported.