Test Configuration
Configuring Test Directory
By default, Patrol looks for tests in thepatrol_test/ directory. This default was changed from integration_test/ to avoid conflicts with Flutter’s official integration testing plugin and to give Patrol tests their own dedicated space.
This change was introduced in Patrol 4.0.0.
Using Custom Test Directory
You can configure Patrol to use a different directory by addingtest_directory to your pubspec.yaml:
pubspec.yaml
Migrating from integration_test Directory
If you have existing Patrol tests in theintegration_test/ directory, you have two options:
Option 1: Rename integration_test directory to patrol_test
Option 1: Rename integration_test directory to patrol_test
Simply rename your existing directory:This is the recommended approach for new projects.
Option 2: Configure Patrol to use integration_test
Option 2: Configure Patrol to use integration_test
Keep using your old directory by updating
pubspec.yaml:pubspec.yaml
Non-patrol integration tests should remain in the
integration_test directory.Security Best Practices
Avoiding Hardcoded Credentials
It’s a bad practice to hardcode data such as emails, usernames, and passwords in test code.Method 1: Using —dart-define
To set environment variables, use--dart-define:
Method 2: Using .patrol.env File
Alternatively, create a.patrol.env file in your project’s root. Comments are supported using the # symbol:
.patrol.env
Working with Permissions
Granting Sensitive Permissions Through Settings
Some particularly sensitive permissions (such as access to background location or controlling the Do Not Disturb feature) cannot be requested in the permission dialog like most common permissions. Instead, you have to ask the user to go to the Settings app. Testing such flows is not as simple as simply granting normal permission, but it’s totally possible with Patrol.Example: Granting Do Not Disturb Permission on Android
Below is a snippet that will make the built-in Camera app have access to the Do Not Disturb feature on Android:Handling Permissions Before Pumping Widget
Sometimes you might want to manually request permissions in the test before the main app widget is pumped. Let’s say that you’re using the geolocator package:See also: Patrol issue #628
Finder Shortcuts
Using Symbol Notation for Keys
Patrol supports a convenient shorthand for keys using Dart symbols:Finding Widgets by Semantics
If you want to use semantics finders to locate widgets by their semantics properties, you can use flutter_test finders inside Patrol finders:Waiting and Timing
Wait for Widget Visibility
Instead of using arbitrary delays, wait for widgets to become visible:Custom Timeouts for Long Operations
Some operations take longer than the default timeout. Specify custom timeouts:Advanced Patterns
Organizing Keys by Page
For larger apps, organize your keys by page or feature:lib/integration_test_keys.dart
Feel free to put your page-specific key classes (e.g.
SignInPageKeys) into separate files in more complex apps.Scrolling to Widgets
When testing lists or scrollable content, you may need to scroll to a widget before interacting with it:Working with Multiple Matching Widgets
When multiple widgets match your finder, use.at(index) to specify which one:
Platform-Specific Tips
Android: Using ADB Commands
Android: Using ADB Commands
You can use ADB commands alongside Patrol tests for advanced scenarios:
iOS: Working with Simulators
iOS: Working with Simulators
Useful iOS simulator commands:
Handling Platform Differences
Handling Platform Differences
Use platform checks when behavior differs between iOS and Android:
Performance Tips
Use patrol develop
For faster iteration during test development:Press ‘r’ to hot restart tests without rebuilding.
Minimize pumpAndSettle
Use
pumpAndSettle() sparingly as it waits for all animations:Reuse Test Setup
Extract common setup into helper functions:
Group Related Tests
Use
group() to organize tests and share setup:Useful Resources
- Patrol Cheat Sheet - Quick reference for common operations
- VS Code Extension - IDE integration for better DX
- DevTools Extension - Monitor test execution in real-time