Patrol

Setting Up Patrol Test

Estimated reading: 4 minutes 142 views
Patrol Featured Image

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

pubspec.yaml
dev_dependencies:
  patrol: ^3.11.0

Once you’ve done this, Patrol will be available for you to run tests in 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.

Installing Patrol CLI via Terminal

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.

Running Patrol Doctor
Running Patrol Doctor

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.

 
Patrol Android Directory Setup
Patrol Android Directory Setup

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"
Patrol DefaultConfig Setup

In the android section, include the following configuration:

 
android/app/build.gradle
testOptions {
  execution "ANDROIDX_TEST_ORCHESTRATOR"
}
Patrol Add TestOptions Snippet
Patrol Add TestOptions Snippet

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.

Patrol androidTestUtil dependencies snippet

iOS Setup

To configure Patrol on iOS, open ios/Runner.xcworkspace in Xcode. 

Patrol ios Directory To Go To 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.

Patrol XCode RunnerUITest.m

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.

Patrol IOS Deployment

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:

Patrol Example Integration Test
bash
flutter build ios --config-only integration_test/example_test.dart
Patrol Build IOS Config

Then go to your ios directory and run:

 
bash
pod install --repo-update
Patrol Repo Update
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
Patrol Xcode Backend Build
Patrol Xcode Backend 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