Flutter Matcher | Test your widget before using it !!

Flutter Matcher :

Flutter apps are designed with widgets and of course just about every monitor utilizes these widgets to make up a final application.So how do you exam these widgets whether or not they perform accordingly or not ??

It is pretty really hard each time to concentrate on every single and each individual widget and check them just before earning application are living isn’t then really do not get worried this web site is for you keep tuned till conclude for exciting updates.

You may have implemented Unittest circumstances and UI tests and now practical experience the magic of related tests tactic which test’s widgets.

Welcome to weblog on flutter widget testing i.,e flutter matcher let us get commenced.

 

pubspec.yaml :

This time no new thing to be extra to this file so get going with even more coding and testing on flutter matcher !!!.

 

major.dart :

We have to have to have some widgets to examination right so let us add some in below.

 

Start out with void primary()

void most important()runApp(MyApp())

 

And now include a class MyApp extending StatelessWidget

class MyApp extends StatelessWidget 
  const MyApp(Vital? crucial) : tremendous(essential: crucial)

  @override
  Widget create(BuildContext context) 
    return MaterialApp(
      dwelling: Scaffold(
        appBar: AppBar(
          title: Text("Widget Testing"),
        ),
        physique: Residence(),
      ),
    )
  

 

Let’s insert a separate class exactly where we will determine our widgets to be tested.

course Home extends StatelessWidget 
  const Dwelling(Crucial? key) : tremendous(important: critical)

  @override
  Widget construct(BuildContext context) 
    return Column(
      children:  [
         TextField(
          decoration: InputDecoration(
            border: OutlineInputBorder(),
            labelText: "Enter Text"),
          ),

        TextButton(onPressed: (), child: Text(""))
      ],
    )
  

 

widget_check.dart :

Just like key.dart widget_examination.dart starts off with a void key exactly where in we specify the examination instances making use of flutter matcher.

void major() 
  //Test instances

 

Syntax of a examination case :

How we specify a examination scenario is it equivalent to unittest conditions ??

 

We require to specify the description and callback to exam widgets.

testWidgets(description, callback)

 

Will need to pump widget on the screen.

await tester.pumpWidget(MyApp())


Find the widget by sort

var textField = discover.byType(TextField)

 

Hope the widget to be discovered

anticipate(textField, findsOneWidget)

 

In this article we have regarded a Matcher  there are distinct sorts of flutter matcher.

 

findsOneWidget : Which will discover just one widget of sort specified.

findNothing : It will verify that no widget discovered.

findNWidgets : It will validate the variety of widgets specified.

findWidgets : Verifies one particular or much more than a single widgets.

 

Insert a exam situation

testWidgets("FindOneWidget", (WidgetTester tester) async
  await tester.pumpWidget(MyApp())

  var textField = find.byType(TextField)
  be expecting(textField, findsOneWidget)
)

 

Comprehensive code :

import 'package:flutter/materials.dart'
import 'package:flutter_basics/most important.dart'
import 'package:flutter_check/flutter_exam.dart'

void primary() 

  testWidgets("FindOneWidget", (WidgetTester tester) async
    await tester.pumpWidget(MyApp())

    var textField = discover.byType(TextField)
    be expecting(textField, findsOneWidget)
  )

  testWidgets("FindsNWidgets", (WidgetTester tester) async
    await tester.pumpWidget(MyApp())

    var textField = come across.byType(TextField)
    hope(textField, findsNWidgets(2))
  )

  testWidgets("FindsNothing", (WidgetTester tester) async
    await tester.pumpWidget(MyApp())

    var textField = find.byType(TextButton)
    anticipate(textField, findsNothing)
  )

  testWidgets("FindsWidgets", (WidgetTester tester) async
    await tester.pumpWidget(MyApp())

    var textField = locate.byType(TextField)
    anticipate(textField, findsWidgets)
  )