Protected: Patrol

Setting Up Patrol Test

Estimated reading: 4 minutes 46 views
patrol-test

This guide will walk you through the necessary steps to set up Patrol, covering both Android and iOS configurations. By the end, your project will be ready to run integration tests with Patrol, so you can ensure your app’s quality on different platforms.

Adding Patrol Dependency

To get started, you’ll need to add the Patrol dependency to your Flutter project. Open your pubspec.yaml file and add the Patrol package under dependencies:

pubspec.yaml
dependencies:
patrol: latest_version

Once you’ve done this, Patrol will be available for you to run tests in your app.

Configure Patrol in pubspec.yaml

Now that the Patrol dependency is added, further configuration in the pubspec.yaml file may be required. This ensures that the testing framework is properly set up and integrates seamlessly with your app.

Install Patrol CLI

With the dependency configured, you need to install Patrol’s CLI to run the tests. Simply open your terminal and run:

pubspec.yaml
dart pub global activate patrol_cli

This command will install the Patrol command-line tools required for your testing.

Verifying with Patrol Doctor

After installing the CLI, run the Patrol Doctor to verify that your project is ready for testing. This handy tool will inspect your project and alert you to any potential issues. Use the following command:

 
pubspec.yaml
patrol doctor

Running this ensures that everything is correctly set up and that you can proceed without any errors.

Android Setup

Setting up Patrol for Android involves a few more steps. First, go to the directory android/app/src/androidTest/java/com/example/myapp/ in your project. If these folders don’t exist, you can create them, but remember to replace /com/example/myapp/ with your app’s package path.

Next, modify your build.gradle file, located in the android/app directory. Add the following lines to the defaultConfig section:

 
android/app/build.gradle
testInstrumentationRunner "pl.leancode.patrol.PatrolJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: "true"

In the android section, include the following configuration:

 
android/app/build.gradle
testOptions {
  execution "ANDROIDX_TEST_ORCHESTRATOR"
}

Finally, add this line to your dependencies:

 
android/app/build.gradle
androidTestUtil "androidx.test:orchestrator:1.4.2"

This setup prepares Patrol to run integration tests on Android, using the orchestrator to manage tests efficiently.

iOS Setup

To configure Patrol on iOS, open ios/Runner.xcworkspace in Xcode. Once the project is open, you’ll notice that two files, RunnerUITests.m and RunnerUITestsLaunchTests.m, are created. 

Delete the RunnerUITestsLaunchTests.m file, as it’s not needed.

Make sure the iOS Deployment Target for RunnerUITests is the same as for Runner. A minimum version of iOS 11.0 is required, though you may need to set this to iOS 13.0 depending on your app dependencies.

If a test target doesn’t already exist, create one by selecting File > New > Target… and choose UI Testing Bundle. Name the product RunnerUITests, and ensure the Organization Identifier is the same as the main app. The target to be tested should be set to Runner.

Replace the contents of RunnerUITests.m with the following code:

RunnerUITests.m
@import XCTest;
@import patrol;
@import ObjectiveC.runtime;

PATROL_INTEGRATION_TEST_IOS_RUNNER(RunnerUITests)

Now, create an empty file called integration_test/example_test.dart in the root of your project. To finalize the setup, run the following commands:

bash
flutter build ios --config-only integration_test/example_test.dart

Then go to your ios directory and run:

 
bash
pod install --repo-update

Open your Xcode project again and verify that the RunnerUITests configurations match those of the Runner. Finally, go to RunnerUITests -> Build Phases and add two new Run Script Phase build phases. Name them xcode_backend build and xcode_backend embed_and_thin, respectively.

For xcode_backend build, paste the following script:

Run Script Phase: xcode_backend build
/bin/sh "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build

And for xcode_backend embed_and_thin:

Runner Build Phase: xcode_backend embed_and_thin
/bin/sh "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed_and_thin

These steps will ensure that your iOS app is ready for Patrol testing.

Conclusion

With Patrol fully set up for both Android and iOS, you’re now ready to run comprehensive mobile tests on your Flutter app. This guide walked you through the process of adding dependencies, configuring your project for Patrol, and setting up your testing environment for both platforms. In the next guide, you’ll learn how to write your first Patrol test, taking your mobile app testing to the next level!

Leave a Comment

Share this Doc

Setting Up Patrol Test

Or copy link

CONTENTS
Review Your Cart
0
Add Coupon Code
Subtotal
Total Installment Payments
Bundle Discount