How to pass parameters into flutter integration_test?

452

If you're using the integration_test package, the test code can set global variables prior to running your app, and pull them from the environment specified using --dart-define

For example:

// In main.dart
var environment = 'production';

void main() {
  if (environment == 'development') {
    // setup configuration for you application
  }

  runApp(const MyApp());
}

// In your integration_test.dart
import 'package:my_app/main.dart' as app;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  setUpAll(() {
    var testingEnvironment = const String.fromEnvironment('TESTING_ENVIRONMENT');
    if (testingEnvironment != null) {
      app.environment = testingEnvironment;
    }
  });

  testWidgets('my test', (WidgetTester tester) async {
    app.main();
    await tester.pumpAndSettle();
    
    // Perform your test
  });
}

then use the command line flutter test integration_test.dart --dart-define TESTING_ENVIRONMENT=development

Alternately, you could pull them from String.fromEnvironment directly in your app code.

Share:
452
Bob
Author by

Bob

Updated on December 22, 2022

Comments

  • Bob
    Bob over 1 year

    I used flutter_driver before for integration tests and was able to insert parameters to the test via environment variables from the host, as the test was running from the host.

    For another project I am now using the integration_test package.

    The test is not running any longer on the host but on the target so when trying to pass arguments via environment variables, the test does not get them.

    I saw https://github.com/flutter/flutter/issues/76852 which I think could help but are there other options available right now?

    • zerotje09
      zerotje09 almost 3 years
      Which parameters are you trying to insert? What are you trying to achieve?
    • Bob
      Bob almost 3 years
      My app talks to BLE devices and I don't want to hardcode the device_addresses as I have to test with different devices. With flutter_driver it was possible to pass them as environment variables.
  • Bob
    Bob about 2 years
    Yes, dart-define and using it with String.fromEnvironment seems the way to go. Unfortunately this means that the app has to be rebuilt with the new settings every time. The old way with flutter_driver even allowed to start the app once and then start the test_driver with different parameters which is way faster.
  • Bob
    Bob about 2 years
    Yes thats true, I also had this issue. Always use const!