Flutter Integration Test for Android and IOS on Firebase Test Lab
I will use the following project as an example of how to run the integration test for both Android and IOS on the Firebase Test lab.
Firebase Test Lab
Firebase Test Lab is a cloud-based app testing infrastructure that lets you test your app on a range of real or virtual devices.
Prerequisite
Ensure you have created a Firebase project. The good news of using Firebase Test Lab is that it’s easy to configure and you don’t have to include any config file or modify your code. 😍
Android
- Create a file named MainActivityTest.java in android/app/src/androidTest/java/com/example/to_do_list/ folder.
It’s fine if you don’t see androidTest folder, just have to create it according to the following folder pattern. (android/app/src/androidTest/java/com/example/to_do_list/)
package com.example.to_do_list;
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);
}
2. Update your application’s build.gradle to make sure it uses androidx’s version of AndroidJUnitRunner
and has androidx libraries as a dependency.
android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}dependencies {
testImplementation 'junit:junit:4.12' // https://developer.android.com/jetpack/androidx/releases/test/#1.2.0
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
I followed with the following documentation but as soon as the flutter build APK command runs. Somehow the directory will change from Android directory to the main directory, therefore I didn’t type the flutter build APK command.
3. Instead, here’s how I did it:
pushd android./gradlew app:assembleAndroidTest
and it will generate the Test APK in the following directory:
build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk
4. Go to Firebase Test Lab, click Run a test and choose Instrumentation.
5. Upload the APK under Test APK.
6. Next, run the following command
./gradlew clean app:assembleDebug -Ptarget=<path_to_local_db_test_file>/integration_test/local_db_test.dart
popd
It will create the debug APK in the following directory: build/app/outputs/flutter-apk/app-debug.apk.
7. Upload the debug APK in APP APK options.
8. Click continue, choose the real device / virtual device, then start the test. You shall be able to see the test result is passed after 1–2 minutes.
IOS
- Create a new Test Target.
2. Add the new test target to ios/Podfile
by embedding in the existing Runner
target.
target 'Runner' do
# Do not change existing lines.
...
target 'RunnerTests' do
inherit! :search_paths
end
end
3. Execute this script at the root of your Flutter app:
output="../build/ios_integ"
product="build/ios_integ/Build/Products"
dev_target="14.3"# Pass --simulator if building for the simulator.
flutter build ios integration_test/foo_test.dart --releasepushd ios
xcodebuild -workspace Runner.xcworkspace -scheme Runner -config Flutter/Release.xcconfig -derivedDataPath $output -sdk iphoneos build-for-testing
popd
pushd $product
zip -r "ios_tests.zip" "Release-iphoneos" "Runner_iphoneos$dev_target-arm64.xctestrun"
popd
4. In Firebase Test Lab, click Run a Test and choose XCTest.
5. Go to <path>/to_do_list/build/ios_integ/Build/Products and upload ios_tests.zip
Since the generated XCTestrun is on IOS14.5 on my PC, therefore I have chosen iPhone 8 with IOS 14.7. Start the test and after a few minutes, you will see the result is pass.
Conclusion
Firebase Test Lab is an amazing and easy-to-use tool to run your test on the cloud. You can view the log and even recorded video of how the test is being done on the device.
I hope this article helps you to publish your app on Firebase Test Lab and feel free to clap for this article or share to whoever of your friend.