slava so
slava so

Reputation: 9

Flutter integration tests: how to run multiple test-classes in the same apk on Firebase Test Lab (e.g. "class1_test.dart", "class2_test.dart"...)?

I have multiple test-classes under folder "integration-test" (e.g. "class1_test.dart", "class2_test.dart"...); I found scripts to generate app-apk and test-apk and run on Firebase TestLab, but these scripts generate test-app for particular test-class, but I would like to run all my tests at once, not only from particular class. How can I do that?

pushd android
flutter build apk 
./gradlew app:assembleAndroidTest
./gradlew app:assembleDebug -Ptarget=integration_test/class1_test.dart #generates apk for 1class 
popd

gcloud firebase test android run \
  --type instrumentation \
  --app build/app/outputs/apk/debug/app-debug.apk \
  --test build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk

Tried to specified few --Ptarget arguments for ./gradlew app:assembleDebug, but anyway it generates test-app with tests only in class1

Upvotes: 1

Views: 1037

Answers (2)

Amit Kumar
Amit Kumar

Reputation: 94

It is pretty simple to execute many test classes together to execute in firebase test lab:

Ex: Lets say below two test classes

class1_test.dart

  void main() {
  group('Test1', () {
    testWidgets('Test case 1', (WidgetTester tester) async {
      // Your test logic here
    });
  });
}

class2_test.dart

     void main() {
  group('Test2', () {
    testWidgets('Test case 2', (WidgetTester tester) async {
      // Your test logic here
    });
  });
}

Now make a single test suite class in which you initialize the test binding and call main methods of above two classes

test_suite.dart file

import '../class1_test.dart' as testclass1;
import '../class2_test.dart' as testclass2;
void main() {
    final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;

//executing all tests
testclass1.main();
testclass2.main();

}

How to call in test app assemble to run in firebase test lab:

    ./gradlew app:assembleDebug -Ptarget=integration_test/test_suite.dart #generates apk for 1class 
popd

This way all of your test cases will run from a single test suite file, if you want to add more test classess , you can add in test suite file in similar way. I hope it works for you

Cheers

Upvotes: 0

Dmytro
Dmytro

Reputation: 141

To run multiple integration tests in the same APK, you can follow these steps:

First, create a separate test file for each test class. For example:

  • integration_test/test1.dart
  • integration_test/test2.dart

In each test file, import the necessary packages and create your test functions. For instance:

import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('Test1', () {
    testWidgets('Test case 1', (WidgetTester tester) async {
      // Your test logic here
    });

    testWidgets('Test case 2', (WidgetTester tester) async {
      // Your test logic here
    });
  });
}

Create a test_driver directory, and within it, create an integration_test.dart file. This file will be responsible for running all your test files:

import 'package:integration_test/integration_test_driver.dart';
import '../integration_test/test1.dart' as test1;
import '../integration_test/test2.dart' as test2;

Future<void> main() async {
  await integrationDriver(test1.main);
  await integrationDriver(test2.main);
}

In your pubspec.yaml file, make sure you have the necessary dependencies and

dev_dependencies:

dependencies:
  integration_test:
    sdk: flutter

dev_dependencies:
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter

Create a test_driver folder (if you haven't already) and, within it, create an app.dart file. This file should import your app's main file and contain the enableFlutterDriverExtension() function, like this:

import 'package:flutter_driver/driver_extension.dart';
import 'package:your_app/main.dart' as app;

void main() {
  enableFlutterDriverExtension();
  app.main();
}

Make sure you have the Flutter Driver package installed by adding the following to your pubspec.yaml under dev_dependencies:

flutter_driver:
  sdk: flutter

Now you can run your integration tests with the following command:

flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app.dart

This command will run all the test files you've specified in the integration_test.dart file in the test_driver directory. The output will show the results of each test case from all test classes.

Upvotes: 0

Related Questions