0
0
Fluttermobile~20 mins

Widget testing in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Widget Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this widget test?
Given this Flutter widget test code, what text does the test find on the screen?
Flutter
testWidgets('Finds Hello Text', (WidgetTester tester) async {
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: Text('Hello'))));
  expect(find.text('Hello'), findsOneWidget);
});
AThe test finds exactly one widget with text 'Hello'.
BThe test finds no widgets with text 'Hello'.
CThe test finds multiple widgets with text 'Hello'.
DThe test throws a runtime error.
Attempts:
2 left
💡 Hint
Look at the widget tree and the text used in the Text widget.
lifecycle
intermediate
2:00remaining
What happens when tester.pump() is called multiple times?
In Flutter widget tests, what is the effect of calling tester.pump() multiple times in a test?
Flutter
testWidgets('Pump multiple times', (WidgetTester tester) async {
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: Text('Step 1'))));
  await tester.pump();
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: Text('Step 2'))));
  await tester.pump();
  expect(find.text('Step 2'), findsOneWidget);
});
ACalling pump multiple times causes a syntax error.
BOnly the first pump call rebuilds the widget tree; others do nothing.
CEach pump rebuilds the widget tree and processes animations or state changes.
DPump calls clear the widget tree and remove all widgets.
Attempts:
2 left
💡 Hint
Think about what pump does in widget testing.
🔧 Debug
advanced
2:00remaining
Why does this widget test fail with a Finder error?
This test fails with an error that no widget was found. What is the cause?
Flutter
testWidgets('Finds RaisedButton', (WidgetTester tester) async {
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: ElevatedButton(onPressed: () {}, child: Text('Press')))));
  expect(find.byType(RaisedButton), findsOneWidget);
});
AThe ElevatedButton widget does not have a child Text widget.
BThe test is missing a call to tester.pump(), so the widget tree is empty.
CThe MaterialApp widget is missing a theme, causing the button not to render.
DRaisedButton is deprecated and replaced by ElevatedButton, so find.byType(RaisedButton) finds nothing.
Attempts:
2 left
💡 Hint
Check the widget types used in the test and the finder.
navigation
advanced
2:00remaining
What is the navigation stack after this widget test code runs?
Given this widget test code that pushes a new route, what is the current route's text after the test runs?
Flutter
testWidgets('Navigation test', (WidgetTester tester) async {
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: Builder(builder: (context) {
    return ElevatedButton(onPressed: () {
      Navigator.push(context, MaterialPageRoute(builder: (_) => Scaffold(body: Text('Second'))));
    }, child: Text('Go'));
  }))));
  await tester.tap(find.text('Go'));
  await tester.pumpAndSettle();
  expect(find.text('Second'), findsOneWidget);
});
AThe current route still shows the text 'Go'.
BThe current route shows the text 'Second'.
CThe test throws a navigation error because context is invalid.
DThe test fails because pumpAndSettle is missing.
Attempts:
2 left
💡 Hint
Consider what Navigator.push and pumpAndSettle do in tests.
🧠 Conceptual
expert
3:00remaining
Which option correctly tests a widget's state change after a button tap?
You have a StatefulWidget that toggles text between 'Off' and 'On' when a button is tapped. Which test code correctly verifies this behavior?
Flutter
class ToggleWidget extends StatefulWidget {
  @override
  State<ToggleWidget> createState() => _ToggleWidgetState();
}
class _ToggleWidgetState extends State<ToggleWidget> {
  bool isOn = false;
  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Text(isOn ? 'On' : 'Off'),
      ElevatedButton(onPressed: () => setState(() => isOn = !isOn), child: Text('Toggle'))
    ]);
  }
}
A
await tester.pumpWidget(MaterialApp(home: ToggleWidget()));
expect(find.text('Off'), findsOneWidget);
await tester.tap(find.text('Toggle'));
await tester.pump();
expect(find.text('On'), findsOneWidget);
B
await tester.pumpWidget(MaterialApp(home: ToggleWidget()));
await tester.tap(find.text('Toggle'));
await tester.pumpAndSettle();
expect(find.text('Off'), findsOneWidget);
C
await tester.pumpWidget(MaterialApp(home: ToggleWidget()));
expect(find.text('On'), findsOneWidget);
await tester.tap(find.text('Toggle'));
await tester.pump();
expect(find.text('Off'), findsOneWidget);
D
await tester.pumpWidget(MaterialApp(home: ToggleWidget()));
await tester.tap(find.text('Toggle'));
expect(find.text('On'), findsOneWidget);
Attempts:
2 left
💡 Hint
Remember to call pump() after state changes to rebuild the widget tree.