Testing in Flutter

Sep 19 2023 · Dart 2.19.3, Flutter 3.7.6, Android Studio 2021.3.1, Visual Studio Code 1.7.4

Part 4: Getting Started With Integration Test

14. Reuse Widget Test in Integration Test

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 13. Test Quotes Page Next episode: 15. Generate Your First Golden

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

In the previous episode, we discussed about widget tests. The difference between a widget test and an integration test is that the widget test runs on Console, and the integration test runs on the device. In this episode, we will learn about integration tests and will be resuing the widget tests into integration tests.

Future<void> loginWidgetTest(
    WidgetTester tester) async {
      
  MockQuotesService mockQuotesService = MockQuotesService();

  await tester.pumpWidget(
    ProviderScope(
      overrides: [
        quotesNotifierProvider
            .overrideWith((ref) => QuotesNotifier(mockQuotesService))
      ],
      child: MaterialApp(
        home: LoginScreen(),
      ),
    ),
  );

when(() => mockQuotesService.getQuotes())
      .thenAnswer((_) async => mockQuotesForTesting);
  await tester.pumpAndSettle();


  final loginText = find.byKey(loginScreenTextKey);
  final emailTextField = find.byKey(emailTextFormKey);
  final passwordTextField = find.byKey(passwordTextFormKey);
  final loginButton = find.byKey(loginButtonKey);

  expect(loginText, findsOneWidget);
  expect(emailTextField, findsOneWidget);
  expect(passwordTextField, findsOneWidget);
  expect(loginButton, findsOneWidget);



  await tester.enterText(emailTextField, 'abcd');
  await tester.enterText(passwordTextField, '1234');

  await tester.tap(loginButton);

  await tester.pumpAndSettle();
  final emailErrorText = find.text(kEmailErrorText);
  final passwordErrorText = find.text(kPasswordErrorText);

  expect(emailErrorText, findsOneWidget);
  expect(passwordErrorText, findsOneWidget);



  await tester.enterText(emailTextField, 'abcd@mail.com');
  await tester.enterText(passwordTextField, 'abcd1234');

  await tester.tap(loginButton);

  await tester.pump(const Duration(seconds: 1));
  expect(find.byKey(loginCircularProgressKey), findsOneWidget);


  await tester.pump(Duration(seconds: 1));
  expect(find.byKey(loginCircularProgressKey), findsNothing);
 
  expect(emailErrorText, findsNothing);
  expect(passwordErrorText, findsNothing);

  await tester.pumpAndSettle();

  var quotesPageTitle = find.byKey(quotesTextKey);
  expect(quotesPageTitle, findsOneWidget);

}
void main(){
    testWidgets('Login Widget Test', loginWidgetTest);
}
void main() {
  testWidgets('All Quotes Widget Test', allQuotesWidgetTest);
}

Future<void> allQuotesWidgetTest(WidgetTester tester) async {
  MockQuotesService mockQuotesService = MockQuotesService();

  void getQuotesAfter2SecondsDelay() {
    when(() => mockQuotesService.getQuotes()).thenAnswer((_) async {
      return await Future.delayed(
          const Duration(seconds: 2), () => mockQuotesForTesting);
    });
  }

  Widget createWidgetUnderTest() {
    return ProviderScope(
      overrides: [
        quotesNotifierProvider.overrideWith(
          (ref) => QuotesNotifier(mockQuotesService),
        ),
      ],
      child: MaterialApp(
        home: AllQuotesScreen(),
      ),
    );
  }

  getQuotesAfter2SecondsDelay();
  await tester.pumpWidget(createWidgetUnderTest());

  expect(find.text('All Quotes'), findsOneWidget);

  await tester.pump(const Duration(seconds: 1));
  expect(find.byKey(quotesCircularProgressKey), findsOneWidget);
  await tester.pumpAndSettle();
  expect(find.byKey(quotesCircularProgressKey), findsNothing);

  expect(find.text('Test Quote 1'), findsOneWidget);
  expect(find.text('Test Quote 2'), findsOneWidget);
  expect(find.text('Test Quote 3'), findsOneWidget);
}
void main(){
    
}
void main(){
    group('Integratoin', () {
        
    });
}
testWidgets('Login-Page', (tester) => loginWidgetTest(tester));
testWidgets('All-Quotes-Page', (tester) => allQuotesWidgetTest(tester));
void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  group('Integration Tests', () {
    testWidgets('Login-Page', (tester) => loginWidgetTest(tester, null));
    testWidgets('All-Quotes-Page', (tester) => allQuotesWidgetTest(tester, null));
  });
}
flutter test integration_test/integration_tests.dart