flutterflutter-dependencies

How to do integration testing in Flutter?


I want to do integration testing in Flutter. The tutorial I follow gives the following procedure:

  1. Add the flutter_driver package to pubspec:
dev_dependencies:
flutter_driver:
sdk: flutter
  1. Enable the Flutter driver extension and add a call to the enableFlutterDriverExtension() function in main.dart.
  2. Run the integration test by using the flutter drive command: flutter drive --target=my_app/test_driver/my_test.dart

My problem is with understanding step 2. It's not clear to me where in Android Studio do you enable the driver extension and where exactly in main.dart do you call the function enableFlutterDriveExtension().

I also have problems with the third step. After running the said command, it says in my terminal that

Error: The Flutter directory is not a clone of the GitHub project.
       The flutter tool requires Git in order to operate properly;
       to set up Flutter, run the following command:
       git clone -b stable https://github.com/flutter/flutter.git

Solution

  • You have to add this code inside the test_driver/app.dart file.

    import 'package:flutter_driver/driver_extension.dart';
    import 'package:[YOUR_APP]/main.dart' as app;
    
    void main() {
      // This line enables the extension
      enableFlutterDriverExtension();
    
      // Call the `main()` function of your app or call `runApp` with any widget you
      // are interested in testing.
      app.main();
    }
    

    You can find more info on the official Flutter documentation site (Steps 3 & 4): https://flutter.dev/docs/cookbook/testing/integration/introduction

    Good luck ;)