text
stringlengths
1
474
so any calls to platform channels need to be mocked in tests.See the test directory for an example.Dart integration tests.
Since integration tests run in the context of a
Flutter application (the example app),
they can test both the Dart and native code,
as well as the interaction between them.
They are also useful for unit testing web implementation
code that needs to run in a browser.These are often the most important tests for a plugin.
However, Dart integration tests can’t interact with native UI,
such as native dialogs or the contents of platform views.See the example/integration_test directory for an example.Native unit tests.
Just as Dart unit tests can test the Dart portions
of a plugin in isolation, native unit tests can
test the native parts in isolation.
Each platform has its own native unit test system,
and the tests are written in the same native languages
as the code it is testing.Native unit tests can be especially valuable
if you need to mock out APIs wrapped by your plugin code,
which isn’t possible in a Dart integration test.You can set up and use any native test frameworks
you are familiar with for each platform,
but the following are already configured in the plugin template:Android:
JUnit tests can be found in android/src/test/.iOS and macOS:
XCTest tests can be found in example/ios/RunnerTests/
and example/macos/RunnerTests/ respectively.
These are in the example directory,
not the top-level package directory,
because they are run via the example app’s project.Linux and Windows:
GoogleTest tests can be found in linux/test/
and windows/test/, respectively.Other types of tests, which aren’t currently pre-configured
in the template, are native UI tests.
Running your application under a native UI testing framework,
such as Espresso or XCUITest,
enables tests that interact with both native and Flutter UI elements,
so can be useful if your plugin can’t be tested without
native UI interactions.<topic_end>
<topic_start>
Running tests
<topic_end>
<topic_start>
Dart unit tests
These can be run like any other Flutter unit tests,
either from your preferred Flutter IDE,
or using flutter test.<topic_end>
<topic_start>
Integration tests
For information on running this type of test, check out the
integration test documentation.
The commands must be run in the example directory.<topic_end>
<topic_start>
Native unit tests
For all platforms, you need to build the example
application at least once before running the unit tests,
to ensure that all of the platform-specific build
files have been created.Android JUnitIf you have the example opened as an Android project
in Android Studio, you can run the unit tests using
the Android Studio test UI.To run the tests from the command line,
use the following command in the example/android directory:iOS and macOS XCTestIf you have the example app opened in Xcode,
you can run the unit tests using the Xcode Test UI.To run the tests from the command line,
use the following command in the example/ios (for iOS)
or example/macos (for macOS) directory:For iOS tests, you might need to first open
Runner.xcworkspace in Xcode to configure code signing.Linux GoogleTestTo run the tests from the command line,
use the following command in the example directory,
replacing “my_plugin” with your plugin project name:If you built the example app in release mode rather than
debug, replace “debug” with “release”.Windows GoogleTestIf you have the example app opened in Visual Studio,
you can run the unit tests using the Visual Studio test UI.To run the tests from the command line,
use the following command in the example directory,
replacing “my_plugin” with your plugin project name:If you built the example app in release mode rather
than debug, replace “Debug” with “Release”.<topic_end>
<topic_start>
What types of tests to add
The general advice for testing Flutter projects
applies to plugins as well.
Some extra considerations for plugin testing:Since only integration tests can test the communication
between Dart and the native languages,
try to have at least one integration test of each
platform channel call.If some flows can’t be tested using integration
tests—for example if they require interacting with
native UI or mocking device state—consider writing
“end to end” tests of the two halves using unit tests:Native unit tests that set up the necessary mocks,
then call into the method channel entry point
with a synthesized call and validate the method response.Dart unit tests that mock the platform channel,
then call the plugin’s public API and validate the results.
<topic_end>
<topic_start>Plugins in Flutter tests
info Note
To learn how to avoid crashes from a plugin when
testing your Flutter app, read on.
To learn how to test your plugin code, check out
Testing plugins.Almost all Flutter plugins have two parts:In fact, the native (or host) language code distinguishes
a plugin package from a standard package.Building and registering the host portion of a plugin
is part of the Flutter application build process,
so plugins only work when your code is running
in your application, such as with flutter run
or when running integration tests.
When running Dart unit tests or
widget tests, the host code isn’t available.
If the code you are testing calls any plugins,
this often results in errors like the following:info Note
Plugin implementations that only use Dart
will work in unit tests. This is an implementation
detail of the plugin, however,
so tests shouldn’t rely on it.When unit testing code that uses plugins,