Flutter Dart Testing On TestMu AI - Android
Flutter, an open-source UI toolkit created by Google, is a popular choice among developers to build natively compiled applications for mobile, web, and desktop from a single codebase. With Dart as its programming language, Flutter enables fast development of beautiful apps with a highly productive, extensible and open-source set of features.
In this guide, we will explore how to run your first Flutter Dart test on an Android device using the TestMu AI Real Device Cloud. By combining the capabilities of Flutter Dart and TestMu AI, you can ensure the proper functioning of your app across different devices and make your app more reliable and robust.
Prerequisites for Getting Started
- Flutter SDK installed on your system
- You will need a TestMu AI username and access key. To obtain your access credentials, purchase a plan or access the automation dashboard.
- Access to an android Sample app (.apk) and an Sample Test Suite app (.apk file).
If you do not have any Flutter Android app (.apk) and an Flutter Test Suite app (.apk) file, you can run your sample tests on TestMu AI by using our sample 🔗 Android app and a sample 🔗 Test Suite.
Run Your First Test
Step 1: Create your Android Flutter app and test suite for testing
For testing, you need to build a Flutter app and test suite. You can create Flutter applications and test suites using either Flutter cli or Gradlew. The steps below demonstrate how to create apks with Gradlew.
-
Create an instrumentation test file in your application's directory
android/app/src/androidTest/java/com/example/lambdatestSampleApp/. Replace com, example, and lambdatestSampleApp values with those from your app's package name.SampleTest.javapackage com.example.lambdatestSampleApp;
import androidx.test.rule.SampleTestRule;
import dev.flutter.plugins.integration_test.FlutterTestRunner;
import org.junit.Rule;
import org.junit.runner.RunWith;
import com.example.lambdatestSampleApp.Sample;
@RunWith(FlutterTestRunner.class)
public class SampleTest {
@Rule
public SampleTestRule<Sample> rule = new SampleTestRule<>(Sample.class, true, false);
} -
Update your application's
lambdatestSampleApp/android/app/build.gradlefile to use androidx's version ofAndroidJUnitRunnerand include theandroidxlibraries as dependencies.build.gradleandroid {
...
defaultConfig {
...
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'
} -
Use the following
Gradlecommands to build an instrumentationtest.apkfile(test suite) using theSample.javacreated in theandroidTestdirectory as mentioned in step 1.Terminal//Go to the android folder which contains the "gradlew" script used for building Android apps from the terminal
pushd android
//Build an Android test APK (uses the Sample.java file created in step 1)
./gradlew app:assembleAndroidTest
//Build a debug APK by passing the integration test file
./gradlew app:assembleDebug -Ptarget="INTEGRATION_TEST_FILE_PATH"
//Go back to the root of the project
popd
Avoiding this step might result in No Tests Ran issue on the dashboard
To create APKs with optional Flutter parameters, first run the Flutter tests in verbose mode with the flutter cli. This allows you to see the Gradle command used internally to build the APKs.
For example, to use --no-sound-null-safety in your tests, run the following command.
flutter run -v --no-sound-null-safety
Next, look for gradlew execution in the logs. The above command generates a gradlew command in the logs that looks something like the following. To build your apk files, replace the parameter YOUR_APP_PATH with your actual path of the application in the following command:
gradlew --full-stacktrace --info -Pverbose=true -Ptarget-platform=android-arm64 -Ptarget=YOUR_APP_PATH/lib/main.dart -Pbase-application-name=android.app.Application -Pdart-obfuscation=false -Pextra-front-end-options=--no-sound-null-safety -Ptrack-widget-creation=true -Ptree-shake-icons=false -Pfilesystem-scheme=org-dartlang-root assembleDebug