0
0
Fluttermobile~10 mins

Test coverage in Flutter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Apackage:flutter/widgets.dart
Bpackage:flutter/material.dart
Cpackage:flutter_test/flutter_test.dart
Dpackage:flutter/src/widgets/framework.dart
Attempts:
3 left
💡 Hint
Common Mistakes
Importing material.dart instead of flutter_test.dart
Forgetting to import any test package
2fill in blank
medium

Complete 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'
Atester
Bwidget
Ccontext
DtesterWidget
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'widget' or 'context'
Not using the WidgetTester parameter at all
3fill in blank
hard

Fix 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'
Apump
Btap
Cbuild
Drebuild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tap()' again instead of 'pump()'
Using non-existent methods like 'rebuild()'
4fill in blank
hard

Fill 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'
Apump
BfindsNothing
CfindsOneWidget
Dtap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tap()' instead of 'pump()' after interaction
Using 'findsOneWidget' when expecting no widget
5fill in blank
hard

Fill 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'
Atester
Bpump
C1
Dtap
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names instead of 'tester'
Forgetting to call 'pump()' after tap
Checking wrong text value