Widget Testing
flutter_test, testWidgets, find, pump, WidgetTester, golden tests, integration tests
1Which package should be used to write widget tests in Flutter?
Which package should be used to write widget tests in Flutter?
답변
The flutter_test package is the official package for writing widget tests in Flutter. It is automatically included in the Flutter SDK and provides all the necessary functions and classes to test widgets, such as testWidgets, find, pump and WidgetTester. This package extends Dart's test package with Flutter-specific features.
2Which function should be used to define a widget test in Flutter?
Which function should be used to define a widget test in Flutter?
답변
The testWidgets function is specifically designed for testing Flutter widgets. It provides a WidgetTester as a parameter to the callback which allows interaction with the widget under test. Unlike test() used for unit tests, testWidgets automatically manages the binding framework needed to render widgets and interact with them.
3How to find a widget by its text in a Flutter test?
How to find a widget by its text in a Flutter test?
답변
The find class provides several methods to locate widgets, including find.text() which searches for a widget containing exactly the specified text. This is one of the most commonly used finders as it allows easily verifying the presence of text on screen. Other common finders include find.byType(), find.byKey() and find.byIcon().
What is the difference between pump() and pumpAndSettle() in widget tests?
How to simulate a tap on a button in a Flutter widget test?
+15 면접 질문