πŸ§ͺTesting

Testing is a crucial part of the development process, ensuring that your code behaves as expected and helping to prevent bugs. Flutter provides a rich set of testing features to test apps at unit, widget, and integration levels.

1. Unit Testing πŸ“Š

Unit testing involves testing individual functions, methods, or classes. The test package provides tools to write unit tests.

Setup:

Add the test package to your pubspec.yaml file:

dev_dependencies:
  test: ^1.24.9

Example:

import 'package:test/test.dart';

void main() {
  test('Addition', () {
    expect(2 + 2, equals(4));
  });
}

2. Widget Testing 🎑

Widget testing involves testing a single widget. The flutter_test package provides tools to create widget tests.

Setup:

The flutter_test package is included in every Flutter app.

Example:

3. Integration Testing πŸ”„

Integration testing involves testing a complete app or a large part of an app.

Setup:

Add the integration_test, and flutter_test packages to your pubspec.yaml file:

Example:

Complete Example Code πŸ“œ

Below is a simple Flutter app with corresponding unit, widget, and integration tests.

The app (lib/main.dart):

Unit Test (test/unit_test.dart):

Widget Test (test/widget_test.dart):

Integration Test (integration_test/app_test.dart):

In this example, you have a simple app with corresponding tests at unit, widget, and integration levels. This sets a good foundation for adding more complex tests as your app grows.


Assignments πŸ“

Last updated

Was this helpful?