An example app that demonstrates how to test your app in Flutter.
This project is used in Devera Flutter course to demonstrate how to test your Flutter app.
The more features your app has, the harder it is to test manually. Automated tests help ensure that your app performs correctly before you publish it, while retaining your feature and bug fix velocity.
Automated testing falls into a few categories:
- A unit test tests a single function, method, or class. Test executed on the unit level, that is, very small (often atomic) pieces of code, such as individual classes or functions. Unit tests verify that one particular unit functions correctly and maintains its "contract" to the outside world in all cases.
- A widget test tests a single widget (in other UI frameworks referred to as component test). Performed on the layer of individual Flutter widgets. Widgets tests verify whether -- or how often -- a widget is present in the widget tree.
- An integration test tests a complete app or a large part of an app. Performed on multiple units of functionality with a focus on verifying the interplay between those units is correct and fulfills an expected outcome.
The Flutter plugins for IntelliJ and VSCode support running tests. This is often the best option while writing tests because it provides the fastest feedback loop as well as the ability to set breakpoints.
IntelliJ
- Open the
fraction_unit_test.dartfile (for example). - Select the
Runmenu. - Click the Run
tests in fraction_unit_test.dartoption (for example). - Alternatively, use the appropriate keyboard shortcut for your platform.
VSCode
- Open the
fraction_unit_test.dartfile (for example). - Select the
Runmenu. - Click the
Start Debuggingoption. - Alternatively, use the appropriate keyboard shortcut for your platform.
You can also use a terminal to run the tests by executing the following command from the root of the project (for example):
flutter test test/basic_unit_test/fraction_unit_test.dart
For the integration test:
flutter drive --target=test_driver/app.dart
For more options regarding unit tests, you can execute this command:
flutter test --help
| Package | Usage |
|---|---|
| HTTP | A composable, multi-platform, Future-based API for HTTP requests. |
| Flutter Bloc | State management with BLoC pattern |
| Provider | State management and context based DI |
| Bloc Test | A testing library which makes it easy to test blocs. Built to be used with the bloc package. |
| test | A full featured library for writing and running Dart tests across platforms. |
| Mockito | A mock framework with APIs for Fakes, Mocks, behavior verification, and stubbing. |
A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
Also, visit the Devera Academy website or Facebook fanpage to find out more about the courses.