29/11/2024

Tech Guru

Trusted Source Technology

Flutter Matcher | Test your widget before using it !!

Flutter Matcher | Test your widget before using it !!

Flutter Matcher :

Flutter applications are created with widgets and certainly each and every screen takes advantage of these widgets to make up a last application.So how do you take a look at these widgets regardless of whether they get the job done appropriately or not ??

It’s fairly hard each individual time to aim on each individual and just about every widget and test them just before making app stay is not then really don’t fear this blog site is for you remain tuned until close for appealing updates.

You may possibly have executed Unittest cases and UI screening and now practical experience the magic of identical tests method which test’s widgets.

Welcome to website on flutter widget testing i.,e flutter matcher let us get started.

 

pubspec.yaml :

This time no new factor to be extra to this file so get likely with more coding and screening on flutter matcher !!!.

 

key.dart :

We will need to have some widgets to exam proper so let us include some in here.

 

Start out with void major()

void major()runApp(MyApp())

 

And now add a class MyApp extending StatelessWidget

class MyApp extends StatelessWidget 
  const MyApp(Crucial? important) : tremendous(vital: crucial)

  @override
  Widget construct(BuildContext context) 
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Widget Tests"),
        ),
        body: Residence(),
      ),
    )
  

 

Let’s include a independent course where by we will outline our widgets to be analyzed.

class Property extends StatelessWidget 
  const Property(Essential? essential) : tremendous(essential: crucial)

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

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

 

widget_test.dart :

Just like most important.dart widget_check.dart starts with a void major in which in we specify the take a look at conditions making use of flutter matcher.

void major() 
  //Exam cases

 

Syntax of a test situation :

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

 

We need to specify the description and callback to test widgets.

testWidgets(description, callback)

 

Need to pump widget on the screen.

await tester.pumpWidget(MyApp())


Uncover the widget by form

var textField = find.byType(TextField)

 

Anticipate the widget to be identified

expect(textField, findsOneWidget)

 

Listed here we have viewed as a Matcher  there are various sorts of flutter matcher.

 

findsOneWidget : Which will obtain specifically a single widget of variety specified.

findNothing : It will validate that no widget uncovered.

findNWidgets : It will verify the quantity of widgets specified.

findWidgets : Verifies one particular or extra than one particular widgets.

 

Include a examination circumstance

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

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

 

Comprehensive code :

import 'package:flutter/product.dart'
import 'package:flutter_essentials/key.dart'
import 'package:flutter_take a look at/flutter_exam.dart'

void major() 

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

    var textField = find.byType(TextField)
    hope(textField, findsOneWidget)
  )

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

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

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

    var textField = come across.byType(TextButton)
    expect(textField, findsNothing)
  )

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

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