Bird
Raised Fist0
Fluttermobile~10 mins

Golden tests for UI in Flutter - Interactive Code Practice

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

Complete the code to import the Flutter golden test package.

Flutter
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';

void main() {
  testWidgets('MyWidget golden test', (WidgetTester tester) async {
    await tester.pumpWidget(const MyWidget());
    await expectLater(find.byType(MyWidget), matchesGoldenFile('[1]'));
  });
}
Drag options to blanks, or click blank then click option'
A'goldens/my_widget.png'
B'assets/image.png'
C'lib/main.dart'
D'test/widget_test.dart'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a Dart or asset file path instead of a golden image path.
Forgetting to wrap the widget with a MaterialApp if needed.
2fill in blank
medium

Complete the code to pump the widget and settle animations before the golden test.

Flutter
await tester.[1](const MyWidget());
await tester.pumpAndSettle();
Drag options to blanks, or click blank then click option'
ApumpWidget
BbuildWidget
CrenderWidget
DloadWidget
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like buildWidget or renderWidget.
Not calling pumpAndSettle after pumpWidget to wait for animations.
3fill in blank
hard

Fix the error in the golden test assertion method.

Flutter
await expectLater(find.byType(MyWidget), [1]('goldens/my_widget.png'));
Drag options to blanks, or click blank then click option'
AmatchesImageFile
BmatchesGoldenImage
CmatchesGoldenFile
DmatchesSnapshot
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect matcher names that cause runtime errors.
Misspelling the method name.
4fill in blank
hard

Fill both blanks to create a golden test that updates the golden file.

Flutter
await expectLater(find.byType(MyWidget), [1]('goldens/my_widget.png',
  [2]: true
));
Drag options to blanks, or click blank then click option'
AmatchesGoldenFile
Bskip
Cskip: false
Dskip: true
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing skip: true with skip: false.
Using incorrect matcher names.
5fill in blank
hard

Fill all three blanks to write a golden test that pumps a widget with a key and compares it.

Flutter
final widget = MyWidget(key: const Key('[1]'));
await tester.[2](widget);
await expectLater(find.byKey(const Key('[3]')), matchesGoldenFile('goldens/my_widget.png'));
Drag options to blanks, or click blank then click option'
AmyWidgetKey
BpumpWidget
DpumpAndSettle
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in widget and find.byKey.
Using pumpAndSettle instead of pumpWidget without animations.

Practice

(1/5)
1. What is the main purpose of a golden test in Flutter UI testing?
easy
A. To compare the current UI rendering with a saved image to detect visual changes
B. To test the performance speed of UI animations
C. To check if the UI code compiles without errors
D. To verify the network requests made by the UI

Solution

  1. Step 1: Understand the purpose of golden tests

    Golden tests compare the UI's current look to a saved image to find visual differences.
  2. Step 2: Eliminate unrelated options

    Performance, compilation, and network checks are not the focus of golden tests.
  3. Final Answer:

    To compare the current UI rendering with a saved image to detect visual changes -> Option A
  4. Quick Check:

    Golden test purpose = Visual comparison [OK]
Hint: Golden tests check UI images, not code or speed [OK]
Common Mistakes:
  • Confusing golden tests with performance tests
  • Thinking golden tests check code syntax
  • Assuming golden tests verify network calls
2. Which Flutter test function is used to compare a widget's UI to a golden file?
easy
A. runApp(widget).toMatchGolden('golden.png')
B. expect(widget, equals('golden.png'))
C. testWidgets('golden test', () => expect(widget, isTrue))
D. expectLater(widget, matchesGoldenFile('golden.png'))

Solution

  1. Step 1: Identify the correct golden test syntax

    The function expectLater with matchesGoldenFile is the standard way to compare UI to a golden image.
  2. Step 2: Check other options for correctness

    Options B, C, and D use incorrect or non-existent syntax for golden tests.
  3. Final Answer:

    expectLater(widget, matchesGoldenFile('golden.png')) -> Option D
  4. Quick Check:

    Golden test syntax = expectLater + matchesGoldenFile [OK]
Hint: Use expectLater with matchesGoldenFile for golden tests [OK]
Common Mistakes:
  • Using expect instead of expectLater
  • Missing matchesGoldenFile matcher
  • Trying to call golden test on runApp directly
3. Given this Flutter golden test snippet:
testWidgets('Button golden test', (tester) async {
  await tester.pumpWidget(MyButton());
  await expectLater(find.byType(MyButton), matchesGoldenFile('button.png'));
});
What will happen if the current button UI differs visually from 'button.png'?
medium
A. The test will throw a syntax error
B. The test will fail indicating a visual mismatch
C. The test will pass regardless of UI differences
D. The test will skip without running

Solution

  1. Step 1: Understand golden test behavior on mismatch

    If the rendered UI does not match the saved golden image, the test fails to alert visual changes.
  2. Step 2: Verify other options

    Passing despite differences, syntax errors, or skipping are not expected behaviors here.
  3. Final Answer:

    The test will fail indicating a visual mismatch -> Option B
  4. Quick Check:

    Golden test mismatch = test failure [OK]
Hint: Golden test fails if UI differs from saved image [OK]
Common Mistakes:
  • Expecting test to pass despite UI changes
  • Confusing runtime errors with test failures
  • Thinking golden tests skip on mismatch
4. Identify the error in this golden test code snippet:
testWidgets('Screen golden test', (tester) async {
  await tester.pumpWidget(MyScreen());
  expect(find.byType(MyScreen), matchesGoldenFile('screen.png'));
});
medium
A. Using expect instead of expectLater for matchesGoldenFile
B. Missing await before tester.pumpWidget
C. Incorrect widget type passed to find.byType
D. Golden file name should be a full path

Solution

  1. Step 1: Check the matcher usage

    Golden tests require expectLater with matchesGoldenFile to work asynchronously.
  2. Step 2: Validate other parts

    Await is correctly used with pumpWidget, widget type is correct, and relative file names are allowed.
  3. Final Answer:

    Using expect instead of expectLater for matchesGoldenFile -> Option A
  4. Quick Check:

    Golden test needs expectLater, not expect [OK]
Hint: Use expectLater, not expect, with matchesGoldenFile [OK]
Common Mistakes:
  • Using expect instead of expectLater
  • Forgetting async/await on pumpWidget
  • Assuming golden file needs full absolute path
5. You want to write a golden test for a button that changes color based on theme. Which approach ensures your golden test covers both light and dark themes correctly?
hard
A. Skip golden tests for theme changes and rely on manual testing
B. Write one golden test with default theme only, assuming dark theme looks similar
C. Write separate golden tests pumping the button with light and dark theme widgets and compare to separate golden files
D. Use a single golden file and toggle theme at runtime without repumping widget

Solution

  1. Step 1: Understand theme impact on UI

    Light and dark themes can change colors significantly, so testing both separately is needed.
  2. Step 2: Evaluate testing approaches

    Separate tests with repumping widgets under each theme and comparing to distinct golden files ensures accurate visual checks.
  3. Final Answer:

    Write separate golden tests pumping the button with light and dark theme widgets and compare to separate golden files -> Option C
  4. Quick Check:

    Test each theme separately with distinct golden files [OK]
Hint: Test light and dark themes separately with different golden files [OK]
Common Mistakes:
  • Assuming one golden file covers all themes
  • Not repumping widget after theme change
  • Skipping golden tests for theme variations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard