text
stringlengths 1
474
|
|---|
In this example, you’ve learned how to use Mockito to test functions or classes
|
that depend on web services or databases. This is only a short introduction to
|
the Mockito library and the concept of mocking. For more information,
|
see the documentation provided by the Mockito package.
|
<topic_end>
|
<topic_start>An introduction to widget testing
|
In the introduction to unit testing recipe,
|
you learned how to test Dart classes using the test package.
|
To test widget classes, you need a few additional tools provided by the
|
flutter_test package, which ships with the Flutter SDK.The flutter_test package provides the following tools for
|
testing widgets:If this sounds overwhelming, don’t worry. Learn how all of these pieces fit
|
together throughout this recipe, which uses the following steps:<topic_end>
|
<topic_start>
|
1. Add the flutter_test dependency
|
Before writing tests, include the flutter_test
|
dependency in the dev_dependencies section of the pubspec.yaml file.
|
If creating a new Flutter project with the command line tools or
|
a code editor, this dependency should already be in place.<topic_end>
|
<topic_start>
|
2. Create a widget to test
|
Next, create a widget for testing. For this recipe,
|
create a widget that displays a title and message.
|
<code_start>class MyWidget extends StatelessWidget {
|
const MyWidget({
|
super.key,
|
required this.title,
|
required this.message,
|
});
|
final String title;
|
final String message;
|
@override
|
Widget build(BuildContext context) {
|
return MaterialApp(
|
title: 'Flutter Demo',
|
home: Scaffold(
|
appBar: AppBar(
|
title: Text(title),
|
),
|
body: Center(
|
child: Text(message),
|
),
|
),
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
3. Create a testWidgets test
|
With a widget to test, begin by writing your first test.
|
Use the testWidgets() function provided by the
|
flutter_test package to define a test.
|
The testWidgets function allows you to define a
|
widget test and creates a WidgetTester to work with.This test verifies that MyWidget displays a given title and message.
|
It is titled accordingly, and it will be populated in the next section.
|
<code_start>void main() {
|
// Define a test. The TestWidgets function also provides a WidgetTester
|
// to work with. The WidgetTester allows you to build and interact
|
// with widgets in the test environment.
|
testWidgets('MyWidget has a title and message', (tester) async {
|
// Test code goes here.
|
});
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
4. Build the widget using the WidgetTester
|
Next, build MyWidget inside the test environment by using the
|
pumpWidget() method provided by WidgetTester.
|
The pumpWidget method builds and renders the provided widget.Create a MyWidget instance that displays “T” as the title
|
and “M” as the message.
|
<code_start>void main() {
|
testWidgets('MyWidget has a title and message', (tester) async {
|
// Create the widget by telling the tester to build it.
|
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
|
});
|
}<code_end>
|
<topic_end>
|
<topic_start>Notes about the pump() methods
|
After the initial call to pumpWidget(), the WidgetTester provides
|
additional ways to rebuild the same widget. This is useful if you’re
|
working with a StatefulWidget or animations.For example, tapping a button calls setState(), but Flutter won’t
|
automatically rebuild your widget in the test environment.
|
Use one of the following methods to ask Flutter to rebuild the widget.info Note
|
To kick off the animation, you need to call pump()
|
once (with no duration specified) to start the ticker.
|
Without it, the animation does not start.These methods provide fine-grained control over the build lifecycle,
|
which is particularly useful while testing.<topic_end>
|
<topic_start>
|
5. Search for our widget using a Finder
|
With a widget in the test environment, search
|
through the widget tree for the title and message
|
Text widgets using a Finder. This allows verification that
|
the widgets are being displayed correctly.For this purpose, use the top-level find()
|
method provided by the flutter_test package to create the Finders.
|
Since you know you’re looking for Text widgets, use the
|
find.text() method.For more information about Finder classes, see the
|
Finding widgets in a widget test recipe.
|
<code_start>void main() {
|
testWidgets('MyWidget has a title and message', (tester) async {
|
await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));
|
// Create the Finders.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.