0
0
Fluttermobile~20 mins

Golden tests for UI in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Golden UI Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What does this golden test verify?
Consider this Flutter golden test code snippet:
testWidgets('Button golden test', (tester) async {
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: ElevatedButton(onPressed: () {}, child: Text('Tap me')))));
  await expectLater(find.byType(ElevatedButton), matchesGoldenFile('button_golden.png'));
});

What is this test checking?
Flutter
testWidgets('Button golden test', (tester) async {
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: ElevatedButton(onPressed: () {}, child: Text('Tap me')))));
  await expectLater(find.byType(ElevatedButton), matchesGoldenFile('button_golden.png'));
});
AIt checks if the ElevatedButton widget looks exactly like the saved image 'button_golden.png'.
BIt verifies the button's onPressed callback is called when tapped.
CIt tests if the button's text is 'Tap me'.
DIt ensures the button is enabled and clickable.
Attempts:
2 left
💡 Hint
Golden tests compare the widget's visual output to a saved image.
lifecycle
intermediate
1:30remaining
When should you update golden files?
You run a golden test and it fails because the UI changed intentionally. What is the correct next step?
AIgnore the failure and keep the old golden file.
BDelete the golden file to stop the test from running.
CUpdate the golden file to the new UI snapshot after verifying the change is expected.
DChange the test code to match the old UI.
Attempts:
2 left
💡 Hint
Golden files represent the expected UI snapshot.
🔧 Debug
advanced
2:30remaining
Why does this golden test fail with a pixel mismatch?
This golden test fails with a pixel mismatch error:
testWidgets('Text widget golden test', (tester) async {
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: Text('Hello'))));
  await expectLater(find.byType(Text), matchesGoldenFile('text_golden.png'));
});

Possible cause?
AThe test runs on different screen sizes or device pixel ratios causing rendering differences.
BThe Text widget is missing a key property causing a crash.
CThe golden file path is incorrect and file is missing.
DThe test does not pump the widget before comparison.
Attempts:
2 left
💡 Hint
Golden tests are sensitive to device pixel ratio and screen size.
🧠 Conceptual
advanced
1:30remaining
What is a key benefit of golden tests in UI development?
Why do developers use golden tests for UI components?
ATo verify user input validation logic.
BTo test backend API responses for UI data.
CTo measure app performance during UI rendering.
DTo detect unintended visual changes early by comparing UI snapshots automatically.
Attempts:
2 left
💡 Hint
Golden tests focus on visual correctness.
📝 Syntax
expert
3:00remaining
Which option correctly sets up a golden test with a custom device pixel ratio?
You want to run a golden test with device pixel ratio 2.0 to avoid pixel mismatch errors. Which code snippet is correct?
Flutter
testWidgets('Custom DPR golden test', (tester) async {
  await tester.binding.setSurfaceSize(Size(200, 200));
  tester.binding.window.devicePixelRatioTestValue = 2.0;
  await tester.pumpWidget(MaterialApp(home: Scaffold(body: Container(color: Colors.red))));
  await expectLater(find.byType(Container), matchesGoldenFile('red_container.png'));
  tester.binding.window.clearDevicePixelRatioTestValue();
  await tester.binding.setSurfaceSize(null);
});
ASetting devicePixelRatioTestValue is not allowed and causes runtime error.
BThe code above correctly sets device pixel ratio and surface size for the golden test.
CYou only need to set surface size; device pixel ratio is ignored in golden tests.
DYou must call tester.binding.window.setDevicePixelRatio(2.0) instead of devicePixelRatioTestValue.
Attempts:
2 left
💡 Hint
Flutter test binding allows overriding device pixel ratio for tests.