【问题标题】:Flutter: how to load file for testingFlutter:如何加载文件进行测试
【发布时间】:2018-01-28 12:48:28
【问题描述】:

文件可以从相对于dart脚本文件的目录中读取,就像var file = new File('./fixture/contacts.json')一样。

但是,在 IDE 中运行的颤振测试无法读取该文件。

Loading as resource(不是,因为我不想在应用程序中捆绑测试数据文件)在测试中也不起作用。

什么是flutter中读取文件的好方法(命令行测试和IDE测试)?

运行flutter test 非常快。但是 Intellij IDE 内部的测试非常慢,但它可以设置调试断点并单步执行和查看变量。所以这两种测试都非常有用。

【问题讨论】:

    标签: testing intellij-idea readfile flutter


    【解决方案1】:

    当前目录在 Flutter 2.0 中始终是项目根目录 ? 因此,您现在可以在命令行和 IDE 中读取具有相同代码的文件。

    https://github.com/flutter/flutter/pull/74622

    【讨论】:

      【解决方案2】:

      只是从Flocbit复制解决方案:https://github.com/flutter/flutter/issues/20907#issuecomment-557634184

      String fixture(String name) {
         var dir = Directory.current.path;
         if (dir.endsWith('/test')) {
           dir = dir.replaceAll('/test', '');
         }
         return File('$dir/test/fixtures/$name').readAsStringSync();
      }
      

      效果很好!

      【讨论】:

        【解决方案3】:

        您可以将测试文件放在两个地方(我知道在两个地方拥有相同的数据不是一个好习惯)。假设我想用final file = File(dataFilename) 在测试代码中打开data.json 文件,那么dataFilename 将是'test_resources/data.json'。如果从 Android Studio 执行测试,则 test_resources 是项目根目录中的文件夹。如果从命令行执行测试 test_resources 是测试文件夹中的一个文件夹。所以把文件放在这两个地方就可以解决问题了。

        【讨论】:

          【解决方案4】:

          我创建了一个 util,因为 vscode 和命令行测试基础路径不同

          https://github.com/terryx/flutter-muscle/blob/master/github_provider/test/utils/test_path.dart

          我就是这样用的

          https://github.com/terryx/flutter-muscle/blob/master/github_provider/test/services/github_test.dart#L13


          另一种方法是在命令行中指定准确的测试路径

          $ flutter test test
          

          【讨论】:

            【解决方案5】:

            刚刚尝试了一下,它比您想象的要容易。

            首先,创建一个与测试位于同一目录中的文件夹。例如,我创建了一个名为 test_resources 的文件夹。

            然后,假设我们有以下 JSON 文件用于测试目的。

            test_resources/contacts.json

            {
              "contacts": [
                {
                  "id": 1,
                  "name": "Seth Ladd"
                },
                {
                  "id": 2,
                  "name": "Eric Seidel"
                }
              ]
            }
            

            test/load_file_test.dart

            我们可以像这样将它用于我们的测试:

            import 'dart:convert';
            import 'dart:io';
            import 'package:flutter_test/flutter_test.dart';
            
            void main() {
              test('Load a file', () async {
                final file = new File('test_resources/contacts.json');
                final json = jsonDecode(await file.readAsString());
                final contacts = json['contacts'];
            
                final seth = contacts.first;
                expect(seth['id'], 1);
                expect(seth['name'], 'Seth Ladd');
            
                final eric = contacts.last;
                expect(eric['id'], 2);
                expect(eric['name'], 'Eric Seidel');
              });
            }
            

            【讨论】:

            • 是我还是这个结构在运行flutter test时不起作用?虽然在 IDE 中工作。任何人都设置为同时工作:命令行和 IDE?
            • @IkarPohorský 看看 Grahambo 的回答。我和你有类似的问题,他的回答为我解决了这个问题。
            • 我更喜欢在测试目录中创建资源目录。通过这种方式,我可以使用这个路径 test/resources/contacts.json 获取资源文件,并且可以将所有测试文件保存在 test 中。
            • @liroKrankka 上面的方法适用于移动和桌面,但是为 Flutter-web 加载 json 文件,因为 'dart:io' 不支持浏览器访问磁盘系统。
            【解决方案6】:

            我今天遇到了同样的问题。我的测试在我的 IDE (Android Studio) 中通过,但在 CI 上失败。我发现 IDE 想要一个非相对路径,但 flutter test 想要一个相对路径。我认为这是一个错误,我会寻找合适的地方来报告它。但是,与此同时,我发现了一个粗略的解决方法。基本上,我有一个函数可以尝试两种方式来指定文件路径..

            import 'dart:convert';
            import 'dart:io';
            
            Future<Map<String, dynamic>> parseRawSample(String filePath,
                [bool relative = true]) async {
              filePath = relative ? "test_data/src/samples/raw/$filePath" : filePath;
            
              String jsonString;
              try {
                jsonString = await File(filePath).readAsString();
              } catch (e) {
                jsonString = await File("../" + filePath).readAsString();
              }
              return json.decode(jsonString);
            }
            
            

            大部分代码都针对我的情况,但我认为其他人可能会根据他们的目的调整此工作。

            【讨论】:

            • 尝试运行flutter test test。不需要相对路径。
            • 我遇到了同样的问题,对我来说它适用于flutter test test
            【解决方案7】:

            您可以使用DefaultAssetBundle

            最终响应 = DefaultAssetBundle.of(context).loadString('assets/file_name.json');

            【讨论】:

            • DefaultAssetBundle 对我来说是未定义的:/
            猜你喜欢
            • 2018-09-03
            • 2019-08-20
            • 1970-01-01
            • 2012-03-15
            • 1970-01-01
            • 1970-01-01
            • 2014-01-11
            • 2013-09-15
            • 2019-09-18
            相关资源
            最近更新 更多