我认为Flutter项目尚不支持.
我将所有非UI代码放在另一个纯Dart程序包中,并将其添加为对Flutter项目的依赖.
对于我的项目,这还有一个优点,就是我可以与浏览器GUI(Angular Dart)共享的代码是分开的,并且不会因Flutter依赖关系而被偶然污染,从而破坏了Web项目.
在此项目中,按照以下步骤操作,我可以在IntellJ中获得覆盖率信息:
您需要“ Dart命令行应用程序” IntelliJ运行配置,而不是“ Dart测试”,“ Flutter”或“ Flutter测试”运行配置.
为了能够使用“ Dart命令行应用程序”运行配置运行测试,您可能需要安装独立的Dart SDK,然后在“偏好设置”>中选择它.语言和框架>飞镖Dart SDK路径.
要使用覆盖率而不是单个文件运行所有测试,您需要一个类似
测试/ 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 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 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 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');
}