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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Integration Testing in Flutter: Getting Started
30 mins
- Getting Started
- Setting Up a Firebase Project
- Setting Up The Android Project
- Setting Up The iOS Project
- Setting Up Authentication and Firestore
- Exploring the Starter Project
- Testing in Flutter
- Comparing Types of Testing
- Examining Unit Testing
- Examining Widget Testing
- Examining Integration Testing
- Setting Up the Project for Testing
- Adding Dependencies
- Creating a Test Directory
- Writing Your First Test
- Diving into LiveTestWidgetsFlutterBinding
- Grouping Tests
- Testing Feature One: Authentication
- Understanding pumpWidget and pumpAndSettle
- Diving Into Widget Keys
- Adding Fake Delays
- Understanding expect()
- Running the Test
- Testing Feature Two: Modifying Ideas
- Inserting New Ideas in a List
- Deleting an Idea
- Where to Go From Here?
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.
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.
Then click Continue again.
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.
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.
Click the iOS button to add the iOS app.
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
.
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.
In the Sign-in method tab, select Email/Password.
Then enable the Email/Password toggle button and click Save.
In the Firestore Database tab, click the Create Database button.
Select Start in test mode and click next.
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:
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.
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.
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.
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.
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.
In this tutorial, you’ll focus on integration testing in Flutter.