prefer-test-structure
Warns when a test is not separated in three sections: arrange, act, assert.
Using those sections in your tests helps keep the same structure for all tests and ensures that tests are not doing multiple rounds of arrange and act.
note
This rule expects a comment for each section (e.g. // arrange
).
Example
❌ Bad:
void main() {
test('bad unit test', () {
// act
final a = 1; // LINT: This test is missing an arrange section before this act section. Try refactoring this test.
final b = 2;
// arrange
final c = a + 1;
// assert
expect(b, c); // LINT: This test is missing an act section before this assert section. Try refactoring this test.
});
}
✅ Good:
void main() {
test('good unit test', () {
// arrange
final a = 1;
final b = 2;
// act
final c = a + 1;
// assert
expect(b, c);
});
}