【发布时间】:2020-10-14 05:33:13
【问题描述】:
有没有办法在flutter集成测试中获取当前项目目录?这是一个基本的测试设置作为示例。
import 'dart:io';
import 'package:flutter_driver/driver_extension.dart';
import 'package:ivori/main.dart' as app;
import 'package:glob/glob.dart';
void main() {
// This line enables the extension.
enableFlutterDriverExtension();
print("Where the working directory? ${Directory.current}");
print("What's under the current directory? ${Glob("*").listSync()}");
// Call the `main()` function of the app, or call `runApp` with
// any widget you are interested in testing.
app.main();
}
中间添加的两行Directory.current 和Glob("*").listSync() 都尝试检查/显示当前工作目录。上面的输出是:
$ flutter drive --target=test_driver/app.dart
Changing current working directory to: /Users/yuchen/Documents/MyDemoApp
Using device iPhone SE (2nd generation).
Starting application: test_driver/app.dart
...
...
...
flutter: Where the working directory? Directory: '/'
flutter: What's under the current directory? [Directory: './home', Directory: './usr', File: './.DS_Store', Directory: './bin', Directory: './sbin', File: './.file',
Directory: './etc', Directory: './var', Directory: './Library', Directory: './System', Link: './.VolumeIcon.icns', Directory: './.fseventsd', Directory: './private',
Directory: './.vol', Directory: './Users', Directory: './Applications', Directory: './opt', Directory: './dev', Directory: './Volumes', Directory: './tmp', Directory:
'./cores']
不难看出,工作目录其实设置为电脑的根文件夹。有没有办法以某种方式进入当前项目目录或 src 目录?
这样做的动机是有一些测试数据将在测试期间使用,但不会在应用程序包中使用。在这个长线程https://github.com/flutter/flutter/issues/12999 中有一些关于单元测试的讨论(修复和工作回合)。但是,它似乎没有集成测试的解决方案。
【问题讨论】:
-
从这个example,
getApplicationDocumentsDirectory();可能会有所帮助 -
感谢您的链接!但
getApplicationDocumentsDirectory用于应用程序的文档文件夹(用于应用程序生成的数据),示例用于模拟以进行测试。但是,我正在寻找一种将应用程序之外的数据用于测试目的的方法。从您共享的链接中,想象如果我想测试应用程序文件夹是否有blah.data文件,我们可以通过那里的MethodChannel大厦模拟它,但是我如何存储这块blah.data?我知道它在我主机上的文件系统上,但是要获取它的相对路径很棘手。 -
是的,相对路径很难获得。
-
@Yuchen,你有没有解决这个问题?
-
@BenjaminLee,不,如果你想出来了,如果你能在这里分享就太好了!
标签: flutter