YouTip LogoYouTip

Flutter Test

This section will introduce three types of testing in Flutter: unit testing, widget testing, and integration testing.\\n\\n* * *\\n\\n## Testing Types Overview\\n\\n| Type | Description | Run Command |\\n| --- | --- | --- |\\n| Unit Test | Test individual functions or class methods | flutter test |\\n| Widget Test | Test the UI of a single Widget | flutter test |\\n| Integration Test | Test complete application flows | flutter test integration_test/ |\\n\\n* * *\\n\\n## Unit Testing\\n\\nUnit testing is used to test business logic code.\\n\\n## Example: Unit Testing\\n\\n// math_utils.dart - Code to be tested\\n\\nclass MathUtils {\\n\\nstatic int add(int a,int b)=> a + b;\\n\\nstatic int subtract(int a,int b)=> a - b;\\n\\nstatic int multiply(int a,int b)=> a * b;\\n\\nstatic double divide(int a,int b){\\n\\nif(b ==0)throw ArgumentError('Cannot Divide By Zero');\\n\\nreturn a / b;\\n\\n}\\n\\nstatic int factorial(int n){\\n\\nif(n <0)throw ArgumentError('No Factorial For Negative Numbers');\\n\\nif(n ==0|| n ==1)return 1;\\n\\nreturn n * factorial(n -1);\\n\\n}\\n\\n}\\n\\n// math_utils_test.dart - Test file\\n\\nimport'package:flutter_test/flutter_test.dart';\\n\\nimport'package:my_app/math_utils.dart';\\n\\nvoid main(){\\n\\n group('MathUtils',(){\\n\\n test('add Add Two Numbers',(){\\n\\n expect(MathUtils.add(2,3), equals(5));\\n\\n});\\n\\ntest('subtract Subtract Two Numbers',(){\\n\\n expect(MathUtils.subtract(5,3), equals(2));\\n\\n});\\n\\ntest('multiply Multiply Two Numbers',(){\\n\\n expect(MathUtils.multiply(4,5), equals(20));\\n\\n});\\n\\ntest('divide Divide Two Numbers',(){\\n\\n expect(MathUtils.divide(10,2), equals(5));\\n\\n});\\n\\ntest('divide Throw Exception On Division By Zero',(){\\n\\n expect(()=> MathUtils.divide(10,0), throwsArgumentError);\\n\\n});\\n\\ntest('factorial Calculate Factorial',(){\\n\\n expect(MathUtils.factorial(5), equals(120));\\n\\n expect(MathUtils.factorial(0), equals(1));\\n\\n});\\n\\ntest('factorial Throw Exception For Negative Numbers',(){\\n\\n expect(()=> MathUtils.factorial(-1), throwsArgumentError);\\n\\n});\\n\\n});\\n\\n}\\n\\n* * *\\n\\n## Widget Testing\\n\\nWidget testing is used to test UI components.\\n\\n## Example: Widget Testing\\n\\n// counter_widget.dart - Widget to be tested\\n\\nclass CounterWidget extends StatefulWidget {\\n\\nconst CounterWidget({super.key});\\n\\n@override\\n\\n State createState()=> _CounterWidgetState();\\n\\n}\\n\\nclass _CounterWidgetState extends State{\\n\\nint _count =0;\\n\\nvoid _increment(){\\n\\n setState((){\\n\\n _count++;\\n\\n});\\n\\n}\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Column(\\n\\n mainAxisSize: MainAxisSize.min,\\n\\n children:[\\n\\n Text('Count: $_count', key:const Key('counter_text')),\\n\\n ElevatedButton(\\n\\n key:const Key('increment_button'),\\n\\n onPressed: _increment,\\n\\n child:const Text('Increase'),\\n\\n),\\n\\n],\\n\\n);\\n\\n}\\n\\n}\\n\\n// counter_widget_test.dart - Test file\\n\\nimport'package:flutter/material.dart';\\n\\nimport'package:flutter_test/flutter_test.dart';\\n\\nimport'package:my_app/counter_widget.dart';\\n\\nvoid main(){\\n\\n testWidgets('CounterWidget Display initial Count',(WidgetTester tester) async {\\n\\n// Build Widget\\n\\n await tester.pumpWidget(const MaterialApp(\\n\\n home: CounterWidget(),\\n\\n));\\n\\n// Verify initial Count display\\n\\n expect(find.text('Count: 0'), findsOneWidget);\\n\\n});\\n\\ntestWidgets('CounterWidget Click the IncreaseCount button',(WidgetTester tester) async {\\n\\n await tester.pumpWidget(const MaterialApp(\\n\\n home: CounterWidget(),\\n\\n));\\n\\n// Find And Tap Button\\n\\n await tester.tap(find.byKey(const Key('increment_button')));\\n\\n await tester.pump();\\n\\n// Verify Count has been updated\\n\\n expect(find.text('Count: 1'), findsOneWidget);\\n\\n// Tap Again\\n\\n await tester.tap(find.byKey(const Key('increment_button')));\\n\\n await tester.pump();\\n\\nexpect(find.text('Count: 2'), findsOneWidget);\\n\\n});\\n\\n}\\n\\n* * *\\n\\n## Integration Testing\\n\\nIntegration testing is used to test complete application functional flows.\\n\\n## Example: Integration Testing\\n\\n// integration_test/app_test.dart\\n\\nimport'package:flutter/material.dart';\\n\\nimport'package:flutter_test/flutter_test.dart';\\n\\nimport'package:integration_test/integration_test.dart';\\n\\nimport'package:my_app/main.dart';\\n\\nvoid main(){\\n\\n IntegrationTestWidgetsFlutterBinding.ensureInitialized();\\n\\ngroup('App Integration Test',(){\\n\\n testWidgets('Complete User Flow Test',(WidgetTester tester) async {\\n\\n// Launch App\\n\\n await tester.pumpWidget(const MyApp());\\n\\n await tester.pumpAndSettle();\\n\\n// Verify Home Screen Display\\n\\n expect(find.text('Home'), findsOneWidget);\\n\\n// Click to navigate to DetailsPage\\n\\n await tester.tap(find.text('Details'));\\n\\n await tester.pumpAndSettle();\\n\\n// Verify DetailsPage display\\n\\n expect(find.text('DetailsPage'), findsOneWidget);\\n\\n// Tap Back\\n\\n await tester.tap(find.byIcon(Icons.arrow_back));\\n\\n await tester.pumpAndSettle();\\n\\n// Verify Return To Home Screen\\n\\n expect(find.text('Home'), findsOneWidget);\\n\\n});\\n\\n});\\n\\n}\\n\\n> Good test coverage can improve code quality. It is recommended to at least write unit tests for core business logic.
← Flutter Best PracticesFlutter Devtools β†’