我认为 Flutter 项目尚不支持。
我将所有非 UI 代码放在另一个纯 Dart 包中,并将其作为依赖项添加到 Flutter 项目中。
对于我的项目来说,这还有一个优点,即我可以与浏览器 GUI (Angular Dart) 共享的代码是分离的,并且不会意外地被 Flutter 依赖项污染,从而破坏 Web 项目。
在此项目中,当我按照以下步骤操作时,我可以在 IntelliJ 中获取覆盖率信息:
您需要“Dart 命令行应用程序”IntelliJ 运行配置,而不是“Dart Test”、“Flutter”或“Flutter Test”运行配置。
为了能够使用“Dart 命令行应用程序”运行配置运行测试,您可能需要安装独立的 Dart SDK,并在首选项 > 语言和框架 > Dart > Dart SDK 路径中选择它。
要运行覆盖范围内的所有测试而不是单个文件,您需要一个类似的文件
test/all.dart
// ignore_for_file: await_only_futures
import 'dart:async';
import 'client/controller/app_controller_test.dart' as i0;
import 'client/controller/authentication_controller_test.dart' as i1;
import 'client/controller/backoffice/backoffice_controller_test.dart' as i2;
import 'client/controller/backoffice/image_reference_controller_test.dart'
as i3;
...
Future<void> main() async {
i0.main();
i1.main();
i2.main();
...
}
每个测试文件都有一个条目。
我使用如下所示的 Grinder 任务自动生成该文件
import 'package:path/path.dart' as path;
...
/// Generate a single Dart file that executes all tests.
/// Dart code coverage reporting still requires that.
@Task('generate test/all.dart')
Future<void> prepareCoverage() async {
final testDir = Directory('test');
final context = path.Context(style: path.Style.posix);
final testFiles = testDir
.listSync(recursive: true, followLinks: false)
.where((e) =>
FileSystemEntity.isFileSync(e.path) && e.path.endsWith('_test.dart'))
.map(
(tf) => context.normalize(path.relative(tf.path, from: testDir.path)))
.toList()
..sort();
final content = StringBuffer('''
// ignore_for_file: await_only_futures
import 'dart:async';
''');
final executions = StringBuffer();
for (var i = 0; i < testFiles.length; i++) {
final testFile = testFiles[i];
content.writeln("import '$testFile' as i$i;");
executions.writeln(' i$i.main();');
}
content
..writeln('Future<void> main() async {')
..writeln()
..writeln(executions)
..writeln('}');
File('test/all.dart').writeAsStringSync(content.toString());
PubApp.global('dart_style')
.run(['-w', '--fix']..add('test/all.dart'), script: 'format');
}