Skip to main content

Run your Flutter integration tests for Android

Integration tests are used to test how individual code works together and to capture the overall performance of an app. The integration_test package, provided by Flutter, is used to write integration tests for your Flutter app.

Currently, BrowserStack allows you to run your Flutter integrations tests on real Android devices.

In this guide, you’ll learn how to:

  1. Setup BrowserStack account
  2. Prepare Android Flutter app and test suite for testing
  3. Upload your app
  4. Upload your test-suite
  5. Run Flutter integration tests

Setup BrowserStack account

You will need a BrowserStack username and access key. To obtain your access credentials, sign up for a free trial or purchase a plan.

Prepare Android Flutter app and test suite for testing

  1. Create an instrumentation test file in your application’s android/app/src/androidTest/java/com/example/myapp/ directory. Replace com, example, and myapp with the values from your app’s package name.

    Then, name this test file as MainActivityTest.java or another name of your choice.
     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;
    
     import com.example.myApp.MainActivity;
    
     @RunWith(FlutterTestRunner.class)
     public class MainActivityTest {
       @Rule
       public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, true, false);
     }
    
  2. Next, update your application’s myapp/android/app/build.gradle file to ensure it uses androidx’s version of AndroidJUnitRunner and includes androidx libraries as a dependency.
     android {
       ...
       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'
     }
    
  3. Use the following Gradle commands to build an instrumentation test.apk file(test suite) using the MainActivityTest.java created in the androidTest directory as mentioned in step 1.
     # 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 MainActivityTest.java file created in step 1)
     ./gradlew app:assembleAndroidTest
    
     # Build a debug APK by passing the integration test file
     ./gradlew app:assembleDebug -Ptarget="$FCI_BUILD_DIR/integration_test/app_test.dart"
    
     # Go back to the root of the project
     popd
    

Upload your app

Upload your Android app (.apk or .aab file) to BrowserStack servers using the REST API request.

The following sample cURL command shows how to upload an app:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/app" \
-F "file=@/path/to/app/file/Application-debug.apk"

A sample response for the request is as follows:


{
    "app_name":"Application-debug.apk",
    "app_url":"bs://j3c874f21852ea50957a3fdc33f47514288c4ba4",
    "app_version":"1.0",
    "app_id":"j3c874f21852ea50957a3fdc33f47514288c4ba4",
    "uploaded_at":"2022-05-12 07:27:54 UTC",
    "expiry":"2022-06-11 07:27:54 UTC"
}

The value of the app_url parameter in the response is used to specify the application under test when you run Flutter integration tests.

Note: App upload will take a few seconds to about a minute, depending on the size of your app. Do not interrupt the curl command until you get the response back.

Upload your test suite

Upload your test suite (.apk) file, created in the previous step, to BrowserStack servers using our REST API request.

The following sample cURL command shows how to upload a test suite:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/test-suite" \
-F "file=@/path/to/app/file/Application-debug-test.apk"

A sample response for the request is as follows:

{
    "test_suite_name":"Application-debug-test.apk",
    "test_suite_url":"bs://f7c874f21852ba57957a3fde31f47514288c4ba4",
    "test_suite_id":"f7c874f21852ba57957a3fde31f47514288c4ba4",
    "uploaded_at":"2022-05-12 07:32:41 UTC",
    "expiry":"2022-06-11 07:32:41 UTC",
    "framework":"flutter-integration-tests"
}

The value of the test_suite_url parameter in the response is used to specify the test suite when you run Flutter Integration tests.

Run Flutter integration tests

After you upload your app and test suite to BrowserStack servers, send an API request to start the test execution as shown in the following sample cURL request:

Select device
This device is not available in the free plan. Please upgrade to paid plan.

Look for the icon to select a real device.

 

A sample response for the request is as follows:

{
    "message" : "Success",
    "build_id" : "4d2b4deb810af077d5aed98f479bfdd2e64f36c3"
}

The build_id is used to uniquely identify your build in App Automate.

View test results

After you start Flutter integration tests execution using the REST API, visit your App Automate dashboard to view your test results, debugging logs, and overall test details.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert