Testing
Unit, widget, and integration tests
Flutter provides three levels of testing: unit, widget, and integration. Each tests at a different scope.
Test pure Dart logic: functions, classes, and business logic. No Flutter framework needed.
// test/math_test.dart
import 'package:test/test.dart';
void main() {
group('Calculator', () {
test('adds two numbers', () {
final calc = Calculator();
expect(calc.add(2, 3), equals(5));
});
test('throws on divide by zero', () {
final calc = Calculator();
expect(
() => calc.divide(1, 0),
throwsA(isA<ArgumentError>()),
);
});
});
}Testing Pyramid
E2E
Widget
Unit
Test Results
Calculator.add2ms
Calculator.divide1ms
Counter increments45ms
Counter decrements38ms
Login flow120ms
Logout flow89ms
5 passed1 failed