【问题标题】:How to pass parameters into flutter integration_test?如何将参数传递给flutter integration_test?
【发布时间】:2021-09-24 03:34:29
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您要插入哪些参数?你想达到什么目的?
  • 我的应用程序与 BLE 设备通信,我不想硬编码 device_addresses,因为我必须使用不同的设备进行测试。使用 flutter_driver 可以将它们作为环境变量传递。

标签: flutter e2e-testing flutter-integration-test


【解决方案1】:

如果您使用的是 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
  });
}

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

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

【讨论】:

    猜你喜欢
    • 2018-08-19
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    • 2021-02-08
    • 1970-01-01
    • 2021-04-24
    • 2020-04-29
    相关资源
    最近更新 更多