text
stringlengths
1
372
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,
there are several options to avoid this exception.
the following solutions are listed in order of preference.
<topic_end>
<topic_start>
wrap the plugin
in most cases, the best approach is to wrap plugin
calls in your own API,
and provide a way of mocking your own API in tests.
this has several advantages:
<topic_end>
<topic_start>
mock the plugin’s public API
if the plugin’s API is already based on class instances,
you can mock it directly, with the following caveats:
<topic_end>
<topic_start>
mock the plugin’s platform interface
if the plugin is a federated plugin,
it will include a platform interface that allows
registering implementations of its internal logic.
you can register a mock of that platform interface
implementation instead of the public API with the
following caveats:
an example of when this might be necessary is
mocking the implementation of a plugin used by
a package that you rely on,
rather than your own code,
so you can’t change how it’s called.
however, if possible,
you should mock the dependency that uses the plugin instead.
<topic_end>
<topic_start>
mock the platform channel
if the plugin uses platform channels,
you can mock the platform channels using
TestDefaultBinaryMessenger.
this should only be used if, for some reason,
none of the methods above are available,
as it has several drawbacks:
because of these limitations, TestDefaultBinaryMessenger
is mainly useful in the internal tests
of plugin implementations,
rather than tests of code using plugins.
you might also want to check out
testing plugins.
<topic_end>
<topic_start>
debugging flutter apps
there’s a wide variety of tools and features to help debug
flutter applications. here are some of the available tools:
<topic_end>
<topic_start>