Challenge - 5 Problems
Flutter Test Coverage 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 Flutter widget test?
Consider this Flutter widget test code. What will the test find when it runs?
Flutter
testWidgets('Finds a Text widget with Hello', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp(home: Scaffold(body: Text('Hello')))); final textFinder = find.text('Hello'); expect(textFinder, findsOneWidget); });
Attempts:
2 left
💡 Hint
Remember that MaterialApp provides necessary context for Text widgets.
✗ Incorrect
The test pumps a MaterialApp with a Scaffold containing a Text widget with 'Hello'. The find.text('Hello') finds exactly one widget, so the expect passes.
❓ lifecycle
intermediate2:00remaining
What is the value of counter after this test runs?
Given this Flutter widget test that increments a counter, what is the final value of counter?
Flutter
int counter = 0; class CounterButton extends StatelessWidget { final VoidCallback onPressed; CounterButton({required this.onPressed}); @override Widget build(BuildContext context) { return ElevatedButton(onPressed: onPressed, child: Text('Increment')); } } testWidgets('Counter increments on button tap', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp(home: CounterButton(onPressed: () { counter++; }))); await tester.tap(find.byType(ElevatedButton)); await tester.pump(); });
Attempts:
2 left
💡 Hint
Check how many times tester.tap is called.
✗ Incorrect
The test taps the ElevatedButton once, triggering the onPressed callback which increments counter from 0 to 1.
📝 Syntax
advanced2:00remaining
What error does this Flutter test code produce?
Analyze this Flutter test code snippet. What error will it cause when run?
Flutter
testWidgets('Missing await on pumpWidget', (WidgetTester tester) { tester.pumpWidget(MaterialApp(home: Text('Test'))); final textFinder = find.text('Test'); expect(textFinder, findsOneWidget); });
Attempts:
2 left
💡 Hint
pumpWidget returns a Future and should be awaited in async tests.
✗ Incorrect
The testWidgets callback is not marked async and pumpWidget is not awaited, so the widget tree is not built before the find and expect calls, causing the test to fail.
🔧 Debug
advanced2:00remaining
Why does this widget test fail to find the button?
This test tries to find a button but fails. What is the cause?
Flutter
testWidgets('Finds button by key', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp(home: Scaffold(body: ElevatedButton(key: Key('btn'), onPressed: () {}, child: Text('Tap me'))))); final buttonFinder = find.byKey(Key('button')); expect(buttonFinder, findsOneWidget); });
Attempts:
2 left
💡 Hint
Check the key strings used in the widget and the finder.
✗ Incorrect
The button's key is 'btn' but the finder looks for key 'button', so it does not find the widget.
🧠 Conceptual
expert2:00remaining
Which option best describes test coverage in Flutter?
Select the statement that correctly explains test coverage in Flutter development.
Attempts:
2 left
💡 Hint
Think about what 'coverage' means in software testing.
✗ Incorrect
Test coverage shows how much of the app's code is tested by automated tests, helping developers find untested code.