Skip to main content

Run your Flutter integration tests for iOS

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.

In this guide, you’ll learn how to:

  1. Setup BrowserStack account
  2. Prepare iOS Flutter test package for testing
  3. Upload your test package
  4. Run iOS Flutter integration tests

Prerequisites

  • You must use Xcode to create and publish your app.
  • Ensure that your application complies with Apple’s Review Guideline before starting the release process.

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 iOS Flutter test package for testing

  1. Launch the Xcode Application.
  2. Open ios/Runner.xcworkspace in your app’s iOS folder on Xcode.
  3. Click File > New > Target.
  4. Create a new Target by clicking on Unit Testing Bundle under the Test Section in the Target window.
  5. Rename the Product Name to RunnerTests in the Target window. Set the Target to be Tested to Runner and set the language Objective-C.
    Note: Ensure that the iOS Deployment Target of RunnerTests file within the Build Settings section is the same as Runner file.
  6. Add a test file RunnerTests.m to a new target file in Xcode. This file acts as the entry point for your tests and executes the flutter wrapper. Add the following code snippet to the file:
     @import XCTest;
     @import integration_test;
    
     INTEGRATION_TEST_IOS_RUNNER(RunnerTests)
    
  7. Add the new test target to ios/Podfile by embedding it in the existing Runner target.
     target 'Runner' do
       #Do not change existing lines.
       ...
    
       target 'RunnerTests' do
         inherit! :search_paths
       end
     end
    
  8. Run the following command to build <file_name>.dart:
     flutter build ios <file_name>.dart --release
    
  9. Execute the following script at the root of your Flutter app:
    output="../build/ios_integration"
    product="build/ios_integration/Build/Products"
    dev_target="<target-name>"
    
    # Pass --simulator if building for the simulator.
    flutter build ios integration_test/<file_name>.dart --release
    
    pushd ios
    xcodebuild -workspace Runner.xcworkspace -scheme Runner -config Flutter/Release.xcconfig -derivedDataPath $output -sdk iphoneos build-for-testing
    popd
    
    pushd $product
    zip -r "<ios_test_package_name>.zip" "Release-iphoneos" "Runner_iphoneos$dev_target-arm64.xctestrun"
    popd
    

Verify locally that your tests are successful by running the following command:

 xcodebuild test-without-building -xctestrun "build/ios_integration/Build/Products/Runner_iphoneos$dev_target-arm64.xctestrun" -destination id=<DEVICE_ID>

Upload your test package

Upload your iOS app test package (<ios_test_package_name> in step 9) in .zip format to BrowserStack servers using the REST API request.

The following sample cURL command shows how to upload the test package:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY"
-X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/ios/test-package"
-F "file=@path/to/your/ios/test/package.zip"

A sample response for the above request is as follows:

{
   "test_package_name":"<ios_test_package_name>.zip",
   "test_package_url":"bs://3f8ca850476a7c26d4698225e32b353c83cac7ed",
   "test_package_id":"3f8ca850476a7c26d4698225e32b353c83cac7ed",
   "uploaded_at":"2022-07-21 14:55:59 UTC",
   "expiry":"2022-08-20 14:55:59 UTC",
   "framework":"flutter-integration-tests",
}

The value of the test_package_url parameter in the response is used to specify the application under test when you run your package.

Run iOS Flutter integration tests

After you upload your test package 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": "f6f6d4214ebc3ccab9814d8090f5838644b0dc72"
}

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
Download Copy