text
stringlengths
1
372
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.
final titleFinder = find.text('T');
final messageFinder = find.text('M');
});
}
<code_end>
<topic_end>
<topic_start>
6. verify the widget using a matcher
finally, verify the title and message text widgets appear on screen
using the matcher constants provided by flutter_test.
matcher classes are a core part of the test package,
and provide a common way to verify a given
value meets expectations.
ensure that the widgets appear on screen exactly one time.
for this purpose, use the findsOneWidget matcher.
<code_start>
void main() {
testWidgets('MyWidget has a title and message', (tester) async {
await tester.pumpWidget(const MyWidget(title: 't', message: 'm'));
final titleFinder = find.text('T');
final messageFinder = find.text('M');
// use the `findsonewidget` matcher provided by flutter_test to verify
// that the text widgets appear exactly once in the widget tree.
expect(titleFinder, findsOneWidget);
expect(messageFinder, findsOneWidget);
});
}
<code_end>
<topic_end>
<topic_start>
additional matchers
in addition to findsOneWidget, flutter_test provides additional
matchers for common cases.
<topic_end>
<topic_start>
complete example