Flutter Matcher | Test your widget before using it !!

Flutter Matcher :

Flutter applications are developed with widgets and yes just about every display employs these widgets to make up a final app.So how do you exam these widgets whether they perform accordingly or not ??

It is pretty tricky each time to concentrate on each individual and every single widget and test them in advance of generating application live is not then really don’t be concerned this blog site is for you stay tuned till conclude for attention-grabbing updates.

You could have applied Unittest conditions and UI tests and now encounter the magic of identical tests method which test’s widgets.

Welcome to weblog on flutter widget screening i.,e flutter matcher let us get begun.

 

pubspec.yaml :

This time no new matter to be extra to this file so get going with additional coding and tests on flutter matcher !!!.

 

key.dart :

We have to have to have some widgets to exam appropriate so let us add some in right here.

 

Start off with void primary()

void principal()runApp(MyApp())

 

And now increase a course MyApp extending StatelessWidget

class MyApp extends StatelessWidget 
  const MyApp(Important? critical) : tremendous(critical: essential)

  @override
  Widget develop(BuildContext context) 
    return MaterialApp(
      residence: Scaffold(
        appBar: AppBar(
          title: Textual content("Widget Screening"),
        ),
        human body: Property(),
      ),
    )
  

 

Let us insert a different course in which we will determine our widgets to be analyzed.

class Residence extends StatelessWidget 
  const Residence(Important? critical) : super(key: essential)

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

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

 

widget_take a look at.dart :

Just like most important.dart widget_test.dart commences with a void major in which in we specify the test cases making use of flutter matcher.

void key() 
  //Exam circumstances

 

Syntax of a exam case :

How we specify a test circumstance is it identical to unittest instances ??

 

We have to have to specify the description and callback to take a look at widgets.

testWidgets(description, callback)

 

Require to pump widget on the display screen.

await tester.pumpWidget(MyApp())


Find the widget by kind

var textField = locate.byType(TextField)

 

Anticipate the widget to be discovered

be expecting(textField, findsOneWidget)

 

Right here we have regarded a Matcher  there are various varieties of flutter matcher.

 

findsOneWidget : Which will discover specifically a person widget of sort specified.

findNothing : It will verify that no widget uncovered.

findNWidgets : It will verify the selection of widgets specified.

findWidgets : Verifies a person or a lot more than 1 widgets.

 

Increase a check scenario

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

  var textField = locate.byType(TextField)
  assume(textField, findsOneWidget)
)

 

Total code :

import 'package:flutter/materials.dart'
import 'package:flutter_fundamental principles/major.dart'
import 'package:flutter_test/flutter_take a look at.dart'

void key() 

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

    var textField = uncover.byType(TextField)
    anticipate(textField, findsOneWidget)
  )

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

    var textField = obtain.byType(TextField)
    be expecting(textField, findsNWidgets(2))
  )

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

    var textField = discover.byType(TextButton)
    be expecting(textField, findsNothing)
  )

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

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

 

 

 

Next Post

How To Use the Vertical Response Status Page

House >> Email Marketing and advertising >> E-mail Internet marketing Agency: How To Use the Vertical Reaction Position Webpage In many industries, e-mail is even now the most successful way to market. It is also a single of the most reliable varieties of communication. At the very least 77% of persons […]
How To Use the Vertical Response Status Page

You May Like