如何将参数传递给flutterintegration_test?

Bob*_*Bob 6 e2e-testing flutter flutter-integration-test

flutter_driver之前用于集成测试,并且能够通过主机中的环境变量将参数插入到测试中,因为测试是从主机运行的。

对于另一个项目,我现在使用integration_test包。

测试不再在主机上运行,​​而是在目标上运行,因此当尝试通过环境变量传递参数时,测试不会获取它们。

我看到https://github.com/flutter/flutter/issues/76852我认为这可能有所帮助,但现在还有其他选择吗?

Jef*_*eff 4

如果您使用的是integration_test包,测试代码可以在运行应用程序之前设置全局变量,并从使用指定的环境中提取它们--dart-define

例如:

// 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
  });
}
Run Code Online (Sandbox Code Playgroud)

然后使用命令行flutter test integration_test.dart --dart-define TESTING_ENVIRONMENT=development

或者,您可以String.fromEnvironment直接从应用程序代码中提取它们。