Creating Integration Tests in Flutter

Flutter Aug 19, 2023

Testing is a fundamental part to ensure the stability and reliability of your Flutter applications. Among the different types of tests available, integration tests stand out as a way to validate the interactions between different components and ensure that the application functions as a whole. This post will take you through the process of setting up and running integration tests in your Flutter projects.

Getting started

To start off, we will create a new project. This makes following along easier and lets us use the default test setup that comes with a fresh Flutter project.

To create a new project navigate to the directory you want to save the project and execute the following command using your preferred command line interface flutter create flutter_integration_test.

Here is an example of what it looks like when you use Git Bash:

executing_flutter_create_integration_test_in_git_bash

Installing

After successfully creating our new project, we need to add the flutter_driver library and integration_test package to our project. The installation process is simple. Navigate to the pubspec.yaml file and add the following lines to your dev_dependencies like so:

dev_dependencies:
  flutter_driver:
    sdk: flutter
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter

Once these lines are added, you can run the command flutter pub get to install these packages.

Creating our integration test

Now that we have the necessary packages installed, let us continue to create our integration test setup. Here are the next steps:

  1. Inside your project's root directory, create a new directory named integration_test.
  2. Copy the widget_test.dart file from the existing test directory and paste it within this newly created directory.
|-- flutter_integration_test/
  |-- integration_test/
    |-- widget_test.dart
  |-- test/
    |-- widget_test.dart

Now we can run our first integration test by executing the following command: flutter test integration_test. This will run every test in the integration_test directory.

If you want to run a single test you can execute the following command: flutter test integration_test/widget_test.dart.
flutter_running_integration_test_by_only_using_integration_test

As you can see, creating and running integration tests is straightforward. However, there is an alternative approach available using the flutter_driver library.

Using Flutter driver

The above approach works and is easier to implement. However, by using the flutter_driver library it is possible to reuse widget tests as integration tests. Also, it seems that the flutter_driver runs tests faster on my machine.

Setting up

To start we need to create the following file inside our root directory: integration_test_driver.dart. With the following code:

import 'package:integration_test/integration_test_driver_extended.dart';

void main() => integrationDriver();

We also need to make slight adjustments to our widget_test.dart file inside our test directory:

import 'package:flutter/material.dart';
import 'package:flutter_integration_test/main.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    await tester.pumpWidget(const MyApp());

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);

    await tester.tap(find.byIcon(Icons.add));
    await tester.pump();

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
  });
}

In this code snippet, we made the following changes:

  1. Added the 'package:integration_test/integration_test.dart'; import.
  2. Called theIntegrationTestWidgetsFlutterBinding.ensureInitialized(); function.

Running the test

To run the test we can execute the following command: flutter drive --driver=integration_test_driver.dart --target=test/widget_test.dart.

flutter_running_integration_test_using_flutter_driver

However, running multiple tests using the flutter_driver library is not as convenient as the previous approach. For every test, we have to run the same command, of course with a different test.

Creating bash script

To simplify this we can use a bash script. For this, we need to create an integration_test.sh file inside our root directory, with the following contents:

flutter drive \
  --driver=integration_test_driver.dart \
  --target=test/widget_test.dart

The \ are necessary when you want to split the command into multiple lines, otherwise the script will look like this:

flutter drive --driver=integration_test_driver.dart --target=test/widget_test.dart

To call multiple tests all we have to do is to repeat the same command with a different test target:

flutter drive \
  --driver=integration_test_driver.dart \
  --target=test/widget_test.dart
flutter drive \
  --driver=integration_test_driver.dart \
  --target=test/other_test.dart

To run our script we can execute bash integration_test.sh. In the GIF below the bash script is executed only with the existing widget_test.dart:

flutter_running_integration_test_using_bash_script

Conclusion

In this post, we have discussed two approaches for creating integration tests in Flutter. We started with the most straightforward approach which includes creating a separate directory for our integration tests and running the tests using flutter test integration_test.

For the second approach, we used the flutter_driver library which comes with the benefit of reusing widget tests in the test directory. For me it also runs the tests faster, so take that into account as well.

If you are planning on not using the flutter_driver package you do not need to include them in your dev_dependencies in your pubspec.yaml file.

Tags