Flutter integration tests on Firebase Test Lab
Flutter is an open-source User Interface SDK that is Software Development Kit. Flutter is an open-source project, and it is maintained by Google. Currently, in March 2021. Google has been released another new version of flutter that is Flutter 2. Flutter as a software development kit is great, but while building a big application, there will be some problems or bugs in the code that has to be debugged. Flutter provides multiple debugging tools such as timeline inspector, memory and performance inspector, and else. These tools ease up the debugging process for a developer, below are listed different tools for debugging flutter apps.
By the way, Flutter has the ability to easily test any application. The ability is that they work the way we want them on a target platform. Dart testing works well for unit and non-UI testing; It runs on the development machine and does not depend on the GUI of the Flutter application
Hello friends, I will talk about my new blog on Flutter integration tests on Firebase Test Lab. We will also implement Flutter integration tests on the Firebase Test Lab demo, and use them in your flutter applications. So let’s get started.
Table Of Contents :
Flutter:
“ Flutter is Google’s UI toolkit that helps you build beautiful and natively combined applications for mobile, web, and desktop in a single codebase in record time, Flutter offers great developer tools, with amazing hot reload”.
What is Integration Test:
We use integration testing to learn how different parts work together. Unlike unit tests, it is intended to be run on devices (physical or virtual) and is used for end-to-end testing in addition to testing individual components. Due to this, these tests are much slower than unit tests, but they are a great way to test whether our app works correctly in real-life scenarios. It is called a test because we use the flutter_driver package and the flutter drive command line to run its GUI.
What is Firebase Test Lab:
Running integration tests is a great way to test our apps, but the equipment we have is limited. Either physical devices can be emulators or simulators, but most of them we need to set up to run tests on our machines. Will have to do This can be problematic if testing has to be done separately on multiple devices. For this we can connect with Firebase Test Lab in this we have access to thousands of devices hosted by Google with different device configurations. All these devices are stored in the cloud, so we can access them remotely. We just need to upload the APK of our app for testing and Firebase will run it.
Implementation:
You need to implement it in your code respectively :
Step 1: Add dependencies.
Add dependencies to pubspec — yaml file.
First, we will add flutter_localization and intl library to pubspec. yaml.
dependencies:integration_test: ^1.0.2+3
Step 2: import the package :
import 'package:integration_test/integration_test.dart';
Step 3: Enable AndriodX
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
Started with integrations tests:
First we need to add IntegrationTestWidgetsFlutterBinding.ensureInitialized() to the method, to add support for integration testing. Because we need this method to initialize the flutter test environment.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_integration_test_lab/main.dart';
import 'package:integration_test/integration_test.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
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);
});
}
Test Running on your machine:
Now for us to run the test on our machine, first we need to create an entry point for the test driver script. To create this we have first created a package named test_driver, inside it, we will create a new file named integration_test.dart and paste it. Which is given in the below code reference.
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
This calldriver for text integration is a feature wrapper around the integrationdriver plugin that enables it to run new tests with the flutter drive command.
$ flutter drive \
--driver=test_driver/integration_test.dart \
--target=test/widget_integration_test.dart
Running Test on Firebase test lab:
Now we will first create a project in the Firebase console for the firebase test lab we don’t need to set up android and ios apps.
Now you can go and set up either Android, iOS, or both apps and prepare them to upload to Firebase Test Lab.
Android Setup:
Now we will set up Android, to set up this we will create a new file android/app/src/androidTest/java/your/package/yourapp/MainActivityTest.java.
package com.pszklarska.flutter_integration_test;
import androidx.test.rule.ActivityTestRule;
import dev.flutter.plugins.integration_test.FlutterTestRunner;
import org.junit.Rule;
import org.junit.runner.RunWith;
@RunWith(FlutterTestRunner.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, true, false);
}
After That, we will run the build. Gradle file for the test of android. Android will add a dependency for testing which will help us to test.android {
defaultConfig {
...
applicationId "com.example.flutter_integration_test_lab"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
...
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
After all, this is set up you can generate the test build with some of the following instrumentation commands given below.
pushd android
flutter build apk
./gradlew app:assembleAndroidTest
./gradlew app:assembleDebug -Ptarget=test/widget_test.dart
popd
Now you will see that as soon as we click on Test Lab a screen opens and in this we click on Browse APK and go to /build/app/outputs/apk/debug/app-debug.apk in our project and Clicking on it then generates this file.
It automatically starts Robo testing as soon as our project is uploaded.
Now we will click on run test in test lab screen in which a pop will open then we will click on instrumentation as we click on it then the instrumentation test screen opens in which you have to upload two already generated apk.
- The App apk located in/build/app/outputs/apk/debug/app-debug.apk.
2. The Test app is located in /build/app/outputs/apk/androidTest/app-debug-androidTest.apk.
After that, we will select our devices and after continuing with this our test will start running. After some time its result will be visible.
Conclusion:
In this article, I have explained Flutter integration tests on Firebase Test Lab, which you can modify and experiment with according to your own, this little introduction was from the Flutter integration tests on Firebase Test Lab.
I hope this blog will provide you with sufficient information on Trying up the Flutter integration tests on Firebase Test Lab. How to integrate flutter to the existing android app? is and work on it in your flutter applications, So please try it.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
Feel free to connect with us
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter-related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.