Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flutter test package needed for writing tests.
Flutter
import '[1]'; void main() { test('Simple test', () { expect(1 + 1, 2); }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing material.dart instead of flutter_test.dart
Forgetting to import any test package
✗ Incorrect
The correct import for Flutter testing utilities is 'package:flutter_test/flutter_test.dart'.
2fill in blank
mediumComplete the code to define a widget test that checks if a Text widget displays the correct string.
Flutter
testWidgets('Check Text widget', (WidgetTester [1]) async { await tester.pumpWidget(const Text('Hello')); expect(find.text('Hello'), findsOneWidget); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'widget' or 'context'
Not using the WidgetTester parameter at all
✗ Incorrect
The testWidgets function provides a WidgetTester object usually named 'tester' to interact with widgets.
3fill in blank
hardFix the error in the test code by completing the missing method to rebuild the widget tree after state changes.
Flutter
testWidgets('Tap button increments counter', (WidgetTester tester) async { await tester.pumpWidget(const MyCounterApp()); await tester.tap(find.byIcon(Icons.add)); await tester.[1](); expect(find.text('1'), findsOneWidget); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tap()' again instead of 'pump()'
Using non-existent methods like 'rebuild()'
✗ Incorrect
After simulating a tap, calling 'pump()' rebuilds the widget tree to reflect changes.
4fill in blank
hardFill both blanks to create a test that verifies a widget is not found after an action.
Flutter
testWidgets('Verify widget disappears', (WidgetTester tester) async { await tester.pumpWidget(const MyWidget()); await tester.tap(find.byKey(Key('removeButton'))); await tester.[1](); expect(find.byType(Text), [2]); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tap()' instead of 'pump()' after interaction
Using 'findsOneWidget' when expecting no widget
✗ Incorrect
After tapping, 'pump()' rebuilds the UI, and 'findsNothing' checks that the widget is gone.
5fill in blank
hardFill all three blanks to write a test that pumps a widget, taps a button, and verifies the counter text updates.
Flutter
testWidgets('Counter increments test', (WidgetTester [1]) async { await [1].pumpWidget(const CounterApp()); await [1].tap(find.byIcon(Icons.add)); await [1].[2](); expect(find.text('[3]'), findsOneWidget); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names instead of 'tester'
Forgetting to call 'pump()' after tap
Checking wrong text value
✗ Incorrect
The WidgetTester is named 'tester'. After tapping, call 'pump()' to rebuild. The counter text should show '1'.