Back to results for “UI Testing”
Flutter › Flutter Plugin & Package Development

How should you properly write both unit tests and platform specific integration tests for a Flutter plugin to genuinely verify it correctly works across every single one of its supported platforms?

Intermediate

Answer

Testing a Flutter plugin thoroughly typically involves writing regular Dart unit tests specifically for the plugin's own shared Dart facing logic, often using a mocked implementation of the underlying platform channel to properly simulate a native response without genuinely needing an actual real physical device, combined with genuinely separate platform specific integration tests that actually run directly on a real device or a properly configured emulator or simulator specifically to verify the real actual native code path genuinely works correctly end to end exactly as intended on each individual supported platform.
test('getBatteryLevel returns a value', () async {
  MyPlugin.setMockMethodCallHandler((call) async => 85);
  expect(await MyPlugin.getBatteryLevel(), 85);
});
Real-world example A battery monitoring plugin's development team writes Dart unit tests using a mocked platform channel response to verify their shared Dart logic, plus genuinely separate platform specific integration tests that actually run directly on both a real Android device and a real iOS device to properly verify the true native implementation genuinely and correctly works.

Common follow-ups: How do you actually properly mock a platform channel's response specifically within a Dart unit test?;What specific CI infrastructure is genuinely needed to actually run these platform specific integration tests automatically?