Flutter Matcher :
Flutter apps are constructed with widgets and sure each and every display screen makes use of those widgets to make up a last app.So how do you take a look at those widgets whether or not they paintings accordingly or now not ??
It’s beautiful onerous each and every time to concentrate on each widget and take a look at them sooner than making app are living isn’t then don’t concern this weblog is for you keep tuned until finish for attention-grabbing updates.
You may have carried out Unittest instances and UI checking out and now enjoy the magic of an identical checking out technique which take a look at’s widgets.
Welcome to weblog on flutter widget checking out i.,e flutter matcher let’s get began.
pubspec.yaml :
This time no new factor to be added to this record so get going with additional coding and checking out on flutter matcher !!!.
primary.dart :
We want to have some widgets to check proper so let’s upload some in right here.
Get started with void primary()
void primary(){runApp(MyApp());}
And now upload a category MyApp extending StatelessWidget
elegance MyApp extends StatelessWidget { const MyApp({Key? key}) : tremendous(key: key); @override Widget construct(BuildContext context) { go back MaterialApp( house: Scaffold( appBar: AppBar( name: Textual content("Widget Checking out"), ), frame: House(), ), ); } }
Let’s upload a separate elegance the place we will be able to outline our widgets to be examined.
elegance House extends StatelessWidget { const House({Key? key}) : tremendous(key: key); @override Widget construct(BuildContext context) { go back Column( kids: [ TextField( decoration: InputDecoration( border: OutlineInputBorder(), labelText: "Enter Text"), ), TextButton(onPressed: (){}, child: Text("")) ], ); } }
widget_test.dart :
Identical to primary.dart widget_test.dart begins with a void primary the place in we specify the take a look at instances the usage of flutter matcher.
void primary() { //Take a look at instances }
Syntax of a take a look at case :
How we specify a take a look at case is it very similar to unittest instances ??
We want to specify the outline and callback to check widgets.
testWidgets(description, callback)
Want to pump widget at the display screen.
wait for tester.pumpWidget(MyApp());
In finding the widget through kind
var textField = in finding.byType(TextField);
Be expecting the widget to be recognized
be expecting(textField, findsOneWidget);
Right here now we have regarded as a Matcher there are various kinds of flutter matcher.
findsOneWidget : Which can in finding precisely one widget of kind specified.
findNothing : It’ll test that no widget discovered.
findNWidgets : It’ll test the choice of widgets specified.
findWidgets : Verifies one or a couple of widgets.
Upload a take a look at case
testWidgets("FindOneWidget", (WidgetTester tester) async{ wait for tester.pumpWidget(MyApp()); var textField = in finding.byType(TextField); be expecting(textField, findsOneWidget); });
Complete code :
import 'package deal:flutter/subject material.dart'; import 'package deal:flutter_basics/primary.dart'; import 'package deal:flutter_test/flutter_test.dart'; void primary() { testWidgets("FindOneWidget", (WidgetTester tester) async{ wait for tester.pumpWidget(MyApp()); var textField = in finding.byType(TextField); be expecting(textField, findsOneWidget); }); testWidgets("FindsNWidgets", (WidgetTester tester) async{ wait for tester.pumpWidget(MyApp()); var textField = in finding.byType(TextField); be expecting(textField, findsNWidgets(2)); }); testWidgets("FindsNothing", (WidgetTester tester) async{ wait for tester.pumpWidget(MyApp()); var textField = in finding.byType(TextButton); be expecting(textField, findsNothing); }); testWidgets("FindsWidgets", (WidgetTester tester) async{ wait for tester.pumpWidget(MyApp()); var textField = in finding.byType(TextField); be expecting(textField, findsWidgets); }); }