Skip to main content

Manage Test Run with Flutter Integration Tests

info

A Flutter report has only one step. If the step fails, it contains the stacktrace and device log.

Android Limitations

info
  • runningType=fastFeedback is not supported with Flutter integration tests. If fastFeedback is used as runningType, the value is ignored and the test runs in coverage mode.
  • Only the  ".apk" extension is supported.

Run Flutter Tests

  1. Create an instrumentation test file in your application's android/app/src/androidTest/java/com/example/myapp/ directory (replacing com, example, and myapp with values from your app's package name). You can name this test file MainActivityTest.java or another name of your choice.

    MainActivityTest.java

    package com.example.myapp;

    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 myapp/android/app/build.gradle to make sure it uses androidx's version of AndroidJUnitRunner and has androidx libraries as a dependency.

    build.gradle

    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'
    }

Build the Application for Running

Use these Gradle commands to build an instrumentation test for Android.

Gradle

pushd android
# flutter build generates files in android/ for building the app
flutter build apk
./gradlew app:assembleAndroidTest
./gradlew app:assembleDebug -Ptarget=<path_to_test>.dart
popd

To submit for a run, run with executionType=Flutter.

iOS Device Testing

  1. Edit tests under integration_test to include the following. It is a workaround for a bug in Flutter with iOS which causes enterText to fail when the app is built in release mode. For more information see the issue.

    import 'dart:io';
    void main() {
    final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
    if (Platform.isIOS) {
    binding.testTextInput.register();
    }
    ...
    }
  2. Open the ios/Runner.xcworkspace in Xcode.

  3. If you do not have a test target, click File > New > Target..., then click Unit Testing Bundle.

  4. Change the Product Name to RunnerTests.

  5. Make sure Target to be Tested is set to Runner and language is set to Objective-C.

  6. Click Finish.

  7. Make sure that the iOS Deployment Target of RunnerTests within the Build Settings section is the same as Runner, including the minimum target version.

  8. 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


To build integration_test/foo_test.dart from the command line:

  1. Run flutter build ios --config-only integration_test/foo_test.dart.

  2. In Xcode, add a test file called RunnerTests.m (or any name of your choice) to the new target and replace the file:

    @import XCTest;
    @import integration_test;

    INTEGRATION_TEST_IOS_RUNNER(RunnerTests)
  3. Click Product > Test to run the integration tests on your selected device.

To deploy it to Continuous Testing Cloud, run this script at the root of your Flutter app:

output="../build/ios_integration"
product="build/ios_integration/Build/Products"

# Pass --simulator if building for the simulator.
flutter build ios integration_test/foo_test.dart --release

pushd ios
xcodebuild build-for-testing \
-workspace Runner.xcworkspace \
-scheme Runner \
-xcconfig Flutter/Release.xcconfig \
-configuration Release \
-derivedDataPath \
$output -sdk iphoneos
popd

pushd $product
mkdir Payload
mv -f Release-iphoneos/Runner.app Payload
zip -r "flutter_test.ipa" "Payload"
popd


For further information see the Flutter documentation.

To submit for a run, run with executionType=Flutter. There is no need to provide the testApp parameter because the built IPA already contains the xctest plugin.

For more information, see Manage Test Run with the API.