π§ͺ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:
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/main.dart'; // Import your main.dart file
void main() {
testWidgets('Finds a text widget', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
expect(find.text('Hello, world!'), findsOneWidget);
});
}
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:
dev_dependencies:
integration_test:
sdk: flutter
flutter_test:
sdk: flutter
Example:
import 'package:integration_test/integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/main.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets("failing test example", (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
expect(find.text('Hello, world!'), findsNothing);
});
}
Complete Example Code π
Below is a simple Flutter app with corresponding unit, widget, and integration tests.
The app (lib/main.dart
):
lib/main.dart
):import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Testing Example')),
body: Center(child: Text('Hello, world!')),
),
);
}
}
Unit Test (test/unit_test.dart
):
test/unit_test.dart
):import 'package:test/test.dart';
void main() {
test('Addition', () {
expect(2 + 2, equals(4));
});
}
Widget Test (test/widget_test.dart
):
test/widget_test.dart
):import 'package:flutter_test/flutter_test.dart';
import 'package:testing_example/main.dart';
void main() {
testWidgets('Finds a text widget', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
expect(find.text('Hello, world!'), findsOneWidget);
});
}
Integration Test (integration_test/app_test.dart
):
integration_test/app_test.dart
):import 'package:integration_test/integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:testing_example/main.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets("finds the text widget", (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
expect(find.text('Hello, world!'), findsOneWidget);
});
}
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?