How to run multiple tests with flutter - Integration Test

2.9k views Asked by At

working with tests in flutter and with the new integration tests package didn't find anything that could help me.

My problem is that I need to run several tests, but when the first test ends or the second test fails, because when the test ends, the application does not restart immediately. My code is like this: and I don't know if this is the best way to do it.

void main() {
 (IntegrationTestWidgetsFlutterBinding.ensureInitialized()
  as IntegrationTestWidgetsFlutterBinding)
  .defaultTestTimeout = const Timeout(Duration(minutes: 2));

WelcomeTester welcomeTester;
LoginTester loginTester;

group('e2e integration test', () {
testWidgets('Test case 1',
        (WidgetTester tester) async {
  await app.main();
  await tester.pumpAndSettle(const Duration(seconds: 5));

  welcomeTester = WelcomeTester(tester);
  loginTester = LoginTester(tester);

  await welcomeTester.checkScreenOpenedByKey(WelcomeKeys.screenWelcomePage);
  await welcomeTester.findTitle();
  await welcomeTester.scrollThePage();
  await welcomeTester.tapButton(WelcomeKeys.btnHaveAccount);
  await welcomeTester.checkScreenOpenedByKey(LoginKeys.screenLoginPage);
  });

testWidgets('Test case 2',
        (WidgetTester tester) async {
  await tester.pumpAndSettle(const Duration(seconds: 5));

  welcomeTester = WelcomeTester(tester);

  await welcomeTester.checkScreenOpenedByKey(WelcomeKeys.screenWelcomePage);
  await welcomeTester.tapButton(WelcomeKeys.btnCreateAccount);
  await welcomeTester.checkScreenOpenedByKey(OnboardingKeys.screenTermsPage);
  });
 });
}

My goal is to create in app_test.dart several test groups and in each group several TestWidgets

2

There are 2 answers

0
Gerry On BEST ANSWER

You need to use this everytime the app changes to give it a chance to move onto the next action.

      await tester.pumpAndSettle();

To settle to UI before moving onto the next action.

Docs: https://api.flutter.dev/flutter/flutter_test/WidgetTester/pumpAndSettle.html

1
Philippe Dupuis On

I'm not totally sure I understand your problem, but one of these solutions might help.

Solution 1

Depending on where you run your tests this might not be a valid answer, but if you are running them locally you could separate your tests into 2 different test files.

Then you could run

flutter drive --flavor=staging --target=integration_test/test_1.dart --driver=test_driver/app_test.dart
flutter drive --flavor=staging --target=integration_test/test_2.dart --driver=test_driver/app_test.dart

This will uninstall and install back your app from the device after each test file is run.

Solution 2

You can also remove the app user data in the tearDown like this

import 'dart:io';
import 'package:path_provider/path_provider.dart';
tearDown(() async {
    final filesDirPath = (await getApplicationSupportDirectory()).path;
    if (Directory(filesDirPath).existsSync()) {
      await Directory(filesDirPath).delete(recursive: true);
    }
});

This should bring back your app in a clean state. There could be more directories to remove like getTemporaryDirectory(), but this is usually where the user data is saved.