flutter-tutorial

Flutter Testing

Testing is a crucial part of an application’s development life cycle. It guarantees that the app is of good quality. Testing necessitates meticulous preparation and execution. It’s also the most time-consuming part of the process.

The Dart programming language and the Flutter framework provide considerable support for automated application testing.

Types of Testing

In general, there are three sorts of testing techniques that can be used to thoroughly test an application. The following is a list of them:

Unit Testing

The simplest way to test an application is to use unit testing. It is based on ensuring the validity of a piece of code (generally, a function) or a class method. However, because it does not mirror the real world, it is the least effective method for locating flaws.

Integration Testing

Integration testing includes both unit and widget testing, as well as external application components like as databases and web services. It almost always finds all bugs by simulating or mocking the real world, although it is the most difficult approach.

All methods of testing are supported by Flutter. It offers comprehensive and exclusive Widget testing support.

Widget Testing

Widget testing ensures that the widget creation, rendering, and interaction with other widgets all work as planned. It takes things a step further by providing a near-real-time environment in which to find even more flaws.

Flutter testing framework provides testWidgets method to test widgets. It accepts two arguments −

  • Test description
  • Test code
testWidgets('test description: find a widget', '<test code>');

Steps Involved

Widget Testing involves three distinct steps −

  • In the testing environment, render the widget.
  • WidgetTester is a Flutter testing framework class that builds and renders widgets. The WidgetTester class’s pumpWidget function accepts any widget and renders it in the testing environment.
testWidgets('finds a specific instance', (WidgetTester tester) async { 
   await tester.pumpWidget(MaterialApp( 
      home: Scaffold( 
         body: Text('Hello'), 
      ), 
   )); 
});

Finding the widget, which we need to test.

Finders are one of the many choices provided by the Flutter framework for finding widgets presented in the testing environment. find.text, find.byKey, and find.byWidget are the most popular finders.

find.text finds the widget that contains the specified text.

find.text('Hello') 

find.byKey find the widget by its specific key.

find.byKey('home') 

find.byWidget find the widget by its instance variable.

find.byWidget(homeWidget) 

Ensuring the widget works as expected.

Matchers are one of the many options provided by the Flutter framework for matching the widget with the intended widget. We can use the testing framework’s expect method to match the widget we found in the second phase with our expected widget by selecting one of the matchers. The following are some of the most important matchers.

findsOneWidget − verifies a single widget is found.

expect(find.text('Hello'), findsOneWidget); 

findsNothing − verifies no widgets are found

expect(find.text('Hello World'), findsNothing); 

findsWidgets − verifies more than a single widget is found.

expect(find.text('Save'), findsWidgets); 

findsNWidgets − verifies N number of widgets are found

expect(find.text('Save'), findsNWidgets(2)); 

The complete test code is as follows −

testWidgets('finds hello widget', (WidgetTester tester) async { 
   await tester.pumpWidget(MaterialApp( 
      home: Scaffold( 
         body: Text('Hello'), 
      ), 
   )); 
   expect(find.text('Hello'), findsOneWidget); 
});

Here, we rendered a MaterialApp widget with text Hello using Text widget in its body. Then, we used find.text to find the widget and then matched it using findsOneWidget.

Working Example

Let us create a simple flutter application and write a widget test to understand better the steps involved and the concept.

  • Create a new flutter application, flutter_test_app in Android studio.
  • Open widget_test.dart in test folder. It has a sample testing code as given below −
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
   // Build our app and trigger a frame. 
   await tester.pumpWidget(MyApp()); 
   
   // Verify that our counter starts at 0. 
   expect(find.text('0'), findsOneWidget); 
   expect(find.text('1'), findsNothing); 
   
   // Tap the '+' icon and trigger a frame. 
   await tester.tap(find.byIcon(Icons.add)); 
   await tester.pump(); 
   
   // Verify that our counter has incremented. 
   expect(find.text('0'), findsNothing); 
   expect(find.text('1'), findsOneWidget); 
});

Here, the test code does the following functionalities −

  • Renders MyApp widget using tester.pumpWidget.
  • Ensures that the counter is initially zero using findsOneWidget and findsNothing matchers.
  • Finds the counter increment button using find.byIcon method.
  • Taps the counter increment button using tester.tap method.
  • Ensures that the counter is increased using findsOneWidget and findsNothing matchers.
  • Let us again tap the counter increment button and then check whether the counter is increased to two.
await tester.tap(find.byIcon(Icons.add));  
await tester.pump();   
expect(find.text('2'), findsOneWidget); 
  • Click Run menu.
  • Click tests in widget_test.dart option. This will run the test and report the result in the result window.

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *