What are integration tests and how to quickly set them up with Flutter 2.5

test

flutter

What are integration tests ? 

These are tests that test a user behavior in the application.


Developers can then test “critical paths” automatically and work with a safety net that ensures they don't break any features.


These tests automate product quality and save a lot of time because they can accurately reproduce human behaviorclicking on a button, expecting to see something, navigating to another page, being able to drag something… 

Today, Flutter is a strong competitor in integration testing since he offers similar features as would do other tools (Detox, Waldo etc.) with the difference that it does not require the use of a third party tool so that all the tests of the application can be centralized !

In this article, I will show you how you can quickly set up your integration tests with Flutter 2.5. 

Getting started

In this article, I will use the brand new skeleton Flutter example app, but you can use the app you are currently working on.

Make sure to have Flutter 2.5.X installed and run:

flutter create -t skeleton my_app


Open it in your favorite IDE (I'll use VSCode), and run it:

code my_app && flutter run

Fichier 2

In this example app we will test when the user tap on one item's list, we navigate to the item details page and we expect to find "more information here". Let's go ! 

Before writing the test, we need to add the flutter package
integration_test in our pubspec.yaml:

dev_dependencies:
    integration_test:
       sdk: flutter
 

Then, create a folder named "integration_test" at the root of your project with a file "tap_on_item_list_navigation_test.dart":

import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

void main() {                   IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  testWidgets("Tap on item list should navigate to item details page",   
  (WidgetTester tester) async {
   expect(1 + 1, 2); 
});}

 

Now, we will check if Flutter has located our integration tests files by running:

flutter test integration_test

 
 

Great ! Now we can start coding our integration test.

your first integration test

We are going to import our app, on top of the test file:

import 'package:my_app/main.dart' as app;

 

Then, we need to tell in our test that we want to start the app:

 
/ *** /
testWidgets("Tap on item list should navigate to item details page",
  (WidgetTester tester) async {
 
  //HERE
  app.main();
  await tester.pumpAndSettle();
 
  expect(1 + 1, 2);
});
 
 

You'll notice that I told the widget tester to "pumpAndSettle" which means: “trigger the next frames until you have no more”. It allows our test suite to make sure our app has finished starting, or an animation finished to play. 

Now we are going to click on the second item of the list.

/ *** /
testWidgets("Tap on item list should navigate to item details page",
  (WidgetTester tester) async {
 
  app.main();
  await tester.pumpAndSettle();
 
  //HERE
     await tester.tap(find.text('SampleItem 2'));
     await tester.pumpAndSettle(); 
   
  expect(1 + 1, 2);
});
 
 

Then we can expect to find "More information here" ! 

testWidgets("Tap on item list should navigate to item details page",
  (WidgetTester tester) async {
 
  app.main();
   await tester.pumpAndSettle();

   await tester.tap(find.text('SampleItem 2'));
   await tester.pumpAndSettle();
     //HERE
   expect(find.text('More Information Here'), findsOneWidget);
});
 
 

Now let's run it again … All tests passed!

 

 

TO go further with integration testing...

As you saw, integration tests with Flutter is a wonderful way to test the features of your application. 


They are easy to set up and allows you to interact with your app in many ways

You can also connect those tests suites to the Firebase Test Lab to run them on real devices in order to reproduce a production environment ! 

I hope you enjoyed this article,

Happy Coding !