FlutterBegin
814 subscribers
370 photos
17 videos
16 files
146 links
Explore the latest in tech, AI, web development, and mobile apps. Stay updated, learn, and grow with us!

Contact: @at_myusername
Download Telegram
๐Ÿงช Unit Testing vs Widget Testing in Flutter

1๏ธโƒฃ Unit Testing
๐Ÿ”น Focus: Tests individual functions, methods, or classes.
๐Ÿ”น Goal: Ensure logic works as expected without UI or dependencies.
๐Ÿ”น Example:
int add(int a, int b) => a + b;

void main() {
test('adds two numbers', () {
expect(add(2, 3), 5);
});
}


๐Ÿ”น Tools: test package.
๐Ÿ”น When to Use: For business logic, utility functions, and data models.



2๏ธโƒฃ Widget Testing
๐Ÿ”น Focus: Tests UI components and user interactions.
๐Ÿ”น Goal: Ensure widgets render correctly and respond to input.
๐Ÿ”น Example:
void main() {
testWidgets('Button increments counter', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());

expect(find.text('0'), findsOneWidget);
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
expect(find.text('1'), findsOneWidget);
});
}


๐Ÿ”น Tools: flutter_test package.
๐Ÿ”น When to Use: For testing widget trees, UI layouts, and interaction flows.



๐Ÿ’ก Tip: Combine Unit and Widget tests to ensure both logic and UI work perfectly! โœ…

@FlutterBegin

#Flutter #Testing
๐Ÿ‘1