WindBreeze
WindBreeze

Reputation: 145

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

Upvotes: 3

Views: 4069

Answers (2)

Nemanja Knežević
Nemanja Knežević

Reputation: 522

In order to run integration test in flutter, you need to create "test_driver" directory inside app root dir. Then you need to create two files inside "test_driver" folder.

  1. Lets call first file "app.dart" and there you need to instrument your app(answer above).

  2. Then you need to create your test file, which needs to be called "app_test.dart" and here you write your actual test code.

  3. When you want to run that test, just run "flutter drive --target=test_driver/app.dart".

    About step 3 in your question, check if you set flutter home properly and after adding flutter_driver dependency, run "packages get".

Upvotes: 0

viskanic
viskanic

Reputation: 36

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 ;)

Upvotes: 1

Related Questions