Skip to main content

avoid-top-level-members-in-tests

added in: 1.6.0
🛠
Free+

Warns when a public top-level member (expect the entrypoint) is declared inside a test file.

Declaring top-level members as private helps reduce code duplication and find unused declarations in test files.

info

To lint only the files that end with _test.dart, set the include configuration for this rule to be test/**/*_test.dart.

Example​

Bad:

final public = 1; // LINT: Avoid top-level declarations in tests. Try making this declaration private.

void function() {} // LINT: Avoid top-level declarations in tests. Try making this declaration private.

class Class {} // LINT: Avoid top-level declarations in tests. Try making this declaration private.

mixin Mixin {} // LINT: Avoid top-level declarations in tests. Try making this declaration private.

extension Extension on String {} // LINT: Avoid top-level declarations in tests. Try making this declaration private.

enum Enum { first, second } // LINT: Avoid top-level declarations in tests. Try making this declaration private.

typedef Public = String; // LINT: Avoid top-level declarations in tests. Try making this declaration private.

Good:

final _private = 2;

void _function() {}

class _Class {}

mixin _Mixin {}

extension _Extension on String {}

enum _Enum { first, second }

typedef _Private = String;