You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成测试中Patrol配置失败问题求助

Flutter Patrol集成测试configure()失败:Connection refused解决方案

核心问题分析

你遇到的SocketException: Connection refused错误,本质是Patrol的Flutter端无法连接到Native端的gRPC服务,通常是测试运行方式或配置不当导致的。

分步解决方法

  1. 使用Patrol CLI运行测试
    不要用普通的flutter test命令,Patrol需要启动Native端的辅助服务,必须用Patrol CLI执行:

    # 安装Patrol CLI(如果未安装)
    dart pub global activate patrol_cli
    # 运行集成测试
    patrol test integration_test/your_test_file.dart
    
  2. 修正NativeAutomator配置

    • 确认packageName和bundleId与项目中实际的Android应用ID、iOS包ID完全一致
    • Android模拟器中localhost需要映射为10.0.2.2,可针对性配置host:
      import 'dart:io';
      
      final automator = NativeAutomator(
        config: NativeAutomatorConfig(
          packageName: 'my.app.android.id',
          bundleId: 'my.app.ios.id',
          host: Platform.isAndroid ? '10.0.2.2' : 'localhost',
        ),
      );
      
  3. 重启相关服务与设备

    • 杀掉所有Flutter、Patrol相关进程
    • 重启模拟器/真机,清除应用缓存后再重新运行测试
  4. 确保Patrol版本兼容
    检查pubspec.yaml中patrol依赖和全局安装的patrol_cli版本一致,建议使用最新稳定版:

    dev_dependencies:
      patrol: ^2.3.0 # 替换为最新稳定版
    

你的代码优化点

  • Firebase.initializeApp()需要添加await,否则可能初始化未完成就执行后续逻辑:
    await Firebase.initializeApp();
    
  • grantPermissionOnlyThisTime()必须指定具体权限,否则调用无效:
    await automator.grantPermissionOnlyThisTime(Permission.location); // 替换为你需要的权限
    
  • 减少Future.delayed的使用,改用pumpAndSettle或等待Widget出现的逻辑,提升测试稳定性:
    // 替换await Future.delayed(...)
    await tester.waitFor(find.byType(LoginBody));
    

内容的提问来源于stack exchange,提问作者Gustawix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 20:35:12