Integration Testing in Flutter: Getting Started

Learn how to test UI widgets along with the backend services in your Flutter project using Integration Testing. By Monikinderjit Singh.

5 (4) · 2 Reviews

Download materials
Save for later
Share

Testing is a must-have skill and a vital part of the software development process. It ensures that your software products are bug-free and meet their specifications.

Michael Bolton’s quote illustrates the importance of testing:

The problem is not that testing is the bottleneck. The problem is that you don’t know what’s in the bottle. That’s a problem that testing addresses.

Companies of all sizes need expert testers. They help streamline your development process while saving you time and money. Learning to test is a win-win scenario.

In this tutorial, you’ll learn about integration testing for your Flutter app. More specifically, you’ll:

  • Learn about different types of testing in Flutter.
  • Write integration tests for Flutter.
  • Access the state of the app.
Note: This tutorial requires basic knowledge of Flutter. If you’re new to Flutter, take a look at our Getting Started with Flutter and Firestore Tutorial for Flutter before continuing. Follow step one and step two and you should be able to run the starter app.

Getting Started

Download the starter project by clicking the Download Materials button at the top or bottom of the tutorial.

The starter project is a complete app. Your primary goal throughout this tutorial is to test it before production.

Open the starter project in VS Code or Android Studio. This tutorial uses VS Code, so you may need to change some of the instructions if you decide to go with Android Studio.

After opening the project, run flutter pub get to install the packages this project uses. Now you’ll set up Firebase and its services for the project.

Setting Up a Firebase Project

Before moving ahead with integration testing, you need to install the Google services configuration file.

Sign in to Firebase. Click Add Project.

Enter the project’s name and then click Continue.

Firebase Project Title

Then click Continue again.

Confirm new Firebase project

You successfully created the Firebase project. Next, you’ll set up Android and iOS projects. Start with Android.

Setting Up The Android Project

The starter project depends on Firebase, so you need to configure your project to access Firebase. You’ll start by configuring the Android project. From your project page in Firebase, click the Android button.

Firebase android

Enter the package name. You can get your package name from android/app/build.gradle.

Click Register App and then download the google-services.json file. Put the google-services.json file in the android/app directory.

That’s it. The remaining necessary code is already in the starter project. Next, you’ll configure your iOS project to access Firebase.

Setting Up The iOS Project

To configure the iOS project to access your Firebase project, follow these steps:

Click Add app to add a new app.

Add new Device Firebase

Click the iOS button to add the iOS app.

Add Firebase IOS device

Enter the bundle ID, the same one you used when setting up the android app. Additionally, you can open ios/Runner.xcodeproj/project.pbxproj and search for PRODUCT_BUNDLE_IDENTIFIER.

IOS Bundle ID

Click Register App and then download GoogleService-Info.plist. Move the file to the ios/Runner folder by replacing the existing GoogleService-Info.plist.

Now, open the iOS project in Xcode. In Xcode, right-click the Runner folder and choose Add files to Runner….

Finally, add GoogleService-Info.plist.

Good job. :]

The rest of the necessary code is already in the starter project. With the devices set up, you only need to set up the services the project uses.

Setting Up Authentication and Firestore

This section will guide you through the steps required to set up Authentication and Firestore services.

In the authentication tab, click Get started.

Firebase Authentication Getting Started

In the Sign-in method tab, select Email/Password.

Firebase sign in screen

Then enable the Email/Password toggle button and click Save.

Enable email/password firebase

In the Firestore Database tab, click the Create Database button.

Firestore Getting Started

Select Start in test mode and click next.

Firestore enable test mode

Click the next.

Bravo! You successfully added the devices. Finally, you need to configure your Firebase project. To do this, head over to the Firebase documentation and follow step one and step two.

Build and run the project. You’ll see the login and sign up page:

Flutter integration Test Starter

You’re ready to explore the starter project.

Exploring the Starter Project

Explore the starter project. You’ll see that it uses Provider for state management and Firebase for authentication and storage.

The home screen displays the user’s ideas. An idea is stored in a collection named ideas in Firestore.

Home page

To insert a new idea, tap the floating action button. Whenever you insert a new idea, the list updates using IdeasModel which extends ChangeNotifierProvider.

To delete an idea, swipe the idea tile horizontally.

Deleting idea

With the steps above, you began to create your tests. In this next section, you’ll learn about testing in Flutter.

Testing in Flutter

While understanding what types of testing are available in Flutter is important, knowing when to use which type is critical.

Currently, there are three categories of testing in Flutter:

  • Unit Testing
  • Widget Testing
  • Integration Testing

Now you’ll learn more about these types by comparing them.

Comparing Types of Testing

You’ll compare the different types of tests available in Flutter to help you decide which type of test is right for you.

You can compare the tests based on multiple parameters:

  • Goal: The test’s ultimate goal.
  • Point to Note: Important point to remember.
  • When to Use: When you should use this type of test.
  • Confidence: Confidence you have in the test’s ability to show that the product does what you expected it to.
  • Speed: The test’s execution speed.

First, you’ll look at unit testing.

Examining Unit Testing

With Unit Testing, your goal is to test the smallest testable piece of code, including, but not limited to classes, and functions. Normally, unit tests run in an isolated environment, where services are mocked with faked data in order to test the output of the testable unit.

Flutter Unit Test

Examining Widget Testing

With Widget Testing, the goal is to assert that a single widget behaves deterministically, based on possibly mocked inputs. Similar to unit testing, Widget tests are normally run in an isolated environment, where input data can be mocked.

Flutter Widget Testing

Examining Integration Testing

Integration tests involve testing multiple testable pieces. Integration tests are often not-run in an isolated environment, and often mimic real-world conditions.

Flutter Integration Testing

In this tutorial, you’ll focus on integration testing in Flutter.