有没有办法在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();
}
Run Code Online (Sandbox Code Playgroud)
中间两行添加的行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']
Run Code Online (Sandbox Code Playgroud)
不难看出,工作目录实际上设置为计算机的根文件夹。有没有办法以某种方式访问当前项目目录或 src 目录?
其背后的动机是获得一些将在测试期间使用但不在应用程序包中使用的测试数据。在这个长线程https://github.com/flutter/flutter/issues/12999中有一些关于单元测试的讨论(修复和工作轮次)。然而,它似乎没有集成测试的解决方案。
就我而言,我使用ext_storage来访问设备的存储。path_provider 插件似乎只能访问应用程序的目录。
以下是获取下载的存储路径的示例。
/// Get storage path for Downloads
/// https://pub.dev/documentation/ext_storage/latest/
Future<String> _getPath() {
return ExtStorage.getExternalStoragePublicDirectory(
ExtStorage.DIRECTORY_DOWNLOADS);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8033 次 |
| 最近记录: |