Challenge - 5 Problems
Widget Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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); });
Attempts:
2 left
💡 Hint
Look at the widget tree and the text used in the Text widget.
✗ Incorrect
The widget tree contains a Text widget with the string 'Hello'. The test looks for exactly one such widget, so it passes.
❓ lifecycle
intermediate2: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); });
Attempts:
2 left
💡 Hint
Think about what pump does in widget testing.
✗ Incorrect
Calling pump rebuilds the widget tree and advances the frame, allowing animations and state changes to process.
🔧 Debug
advanced2: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); });
Attempts:
2 left
💡 Hint
Check the widget types used in the test and the finder.
✗ Incorrect
RaisedButton was replaced by ElevatedButton in Flutter. The finder looks for RaisedButton which does not exist in the widget tree, causing the test to fail.
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); });
Attempts:
2 left
💡 Hint
Consider what Navigator.push and pumpAndSettle do in tests.
✗ Incorrect
The button tap pushes a new route with text 'Second'. pumpAndSettle waits for animations to finish, so the new route is visible and found.
🧠 Conceptual
expert3: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')) ]); } }
Attempts:
2 left
💡 Hint
Remember to call pump() after state changes to rebuild the widget tree.
✗ Incorrect
Option A correctly pumps the widget, verifies initial text, taps the button, pumps again to rebuild, then verifies the changed text.