text
stringlengths
1
372
<code_start>
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// define a test. the TestWidgets function also provides a WidgetTester
// to work with. the WidgetTester allows building and interacting
// with widgets in the test environment.
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'));
// create the finders.
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);
});
}
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>
find widgets
to locate widgets in a test environment, use the finder
classes. while it’s possible to write your own finder classes,
it’s generally more convenient to locate widgets using the tools
provided by the flutter_test package.
during a flutter run session on a widget test, you can also
interactively tap parts of the screen for the flutter tool to
print the suggested finder.
this recipe looks at the find constant provided by
the flutter_test package, and demonstrates how
to work with some of the finders it provides.
for a full list of available finders,
see the CommonFinders documentation.
if you’re unfamiliar with widget testing and the role of
finder classes,
review the introduction to widget testing recipe.
this recipe uses the following steps:
<topic_end>
<topic_start>
1. find a text widget
in testing, you often need to find widgets that contain specific text.
this is exactly what the find.text() method is for. it creates a
finder that searches for widgets that display a specific string of text.
<code_start>
testWidgets('finds a text widget', (tester) async {
// build an app with a text widget that displays the letter 'h'.
await tester.pumpWidget(const MaterialApp(
home: scaffold(
body: Text('H'),
),
));
// find a widget that displays the letter 'h'.
expect(find.text('H'), findsOneWidget);
});
<code_end>
<topic_end>
<topic_start>
2. find a widget with a specific key
in some cases, you might want to find a widget based on the key that has been
provided to it. this can be handy if displaying multiple instances of the
same widget. for example, a ListView might display several
text widgets that contain the same text.
in this case, provide a key to each widget in the list. this allows
an app to uniquely identify a specific widget, making it easier to find
the widget in the test environment.
<code_start>
testWidgets('finds a widget using a key', (tester) async {
// define the test key.
const testKey = Key('K');
// build a MaterialApp with the testKey.
await tester.pumpWidget(MaterialApp(key: testKey, home: container()));
// find the MaterialApp widget using the testKey.
expect(find.byKey(testKey), findsOneWidget);
});
<code_end>