Flutterは複数のテストタイプをサポートしています — ユニットテスト(ロジック)、ウィジェットテスト(UIコンポーネント)、統合テスト(フルアプリフロー)。優れたテスト戦略は信頼性と自信を向上させ、Flutterのテストツールはそれを実用的にします。
ユニットテスト — ロジックをテストする
// test pure logic (functions, classes, business logic) — fast, no UI
test('adds two numbers', () {
expect(add(2, 3), 5);
});
test('Cart calculates total', () {
final cart = Cart()..add(Item(price: 10));
expect(cart.total, 10);
});
ウィジェットテスト — UIコンポーネントをテストする
// widget tests verify a widget's UI and behavior (in a test environment, fast)
testWidgets('Counter increments', (WidgetTester tester) async {
await tester.pumpWidget(MyApp()); // render the widget
expect(find.text('0'), findsOneWidget); // verify initial state
await tester.tap(find.byIcon(Icons.add)); // interact (tap)
await tester.pump(); // rebuild after the change
expect(find.text('1'), findsOneWidget); // verify the update
});
