Loading...
Loading...
Add Flutter unit tests, widget tests, or integration tests
npx skill4agent add flutter/skills flutter-testingtest/_test.dartimport 'package:test/test.dart';
// Import your ViewModel and Fakes here
void main() {
group('HomeViewModel tests', () {
test('Load bookings successfully', () {
final viewModel = HomeViewModel(
bookingRepository: FakeBookingRepository()..createBooking(kBooking),
userRepository: FakeUserRepository(),
);
expect(viewModel.bookings.isNotEmpty, true);
});
});
}import 'package:test/test.dart';
// Import your Repository and Fakes here
void main() {
group('BookingRepositoryRemote tests', () {
late BookingRepository bookingRepository;
late FakeApiClient fakeApiClient;
setUp(() {
fakeApiClient = FakeApiClient();
bookingRepository = BookingRepositoryRemote(apiClient: fakeApiClient);
});
test('should get booking', () async {
final result = await bookingRepository.getBooking(0);
final booking = result.asOk.value;
expect(booking, kBooking);
});
});
}test/flutter_testWidgetTesterFinderfind.text()find.byKey()find.byWidget()MatcherfindsOneWidgetfindsNothingfindsNWidgetsimport 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('HomeScreen displays title and handles tap', (WidgetTester tester) async {
// 1. Setup Fakes and ViewModel
final bookingRepository = FakeBookingRepository()..createBooking(kBooking);
final viewModel = HomeViewModel(
bookingRepository: bookingRepository,
userRepository: FakeUserRepository(),
);
// 2. Build the Widget tree
await tester.pumpWidget(
MaterialApp(
home: HomeScreen(viewModel: viewModel),
),
);
// 3. Finders
final titleFinder = find.text('Home');
final buttonFinder = find.byKey(const Key('increment_button'));
// 4. Assertions
expect(titleFinder, findsOneWidget);
// 5. Interactions
await tester.tap(buttonFinder);
await tester.pumpAndSettle(); // Wait for animations/state updates to finish
expect(find.text('1'), findsOneWidget);
});
}integration_test/integration_testdev_dependenciespubspec.yamlIntegrationTestWidgetsFlutterBindingimport 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('End-to-End App Test', () {
testWidgets('Full flow: tap FAB and verify counter', (WidgetTester tester) async {
// Load the full app
app.main();
await tester.pumpAndSettle();
// Verify initial state
expect(find.text('0'), findsOneWidget);
// Find and tap the FAB
final fab = find.byKey(const ValueKey('increment'));
await tester.tap(fab);
// Trigger a frame
await tester.pumpAndSettle();
// Verify state change
expect(find.text('1'), findsOneWidget);
});
});
}android/src/test/example/ios/RunnerTests/linux/test/flutter test test/your_test_file.dartflutter test integration_test/your_test_file.dart_test.darttester.pump()tester.pumpAndSettle()dart:mirrorsbuild_runnermockitomocktail