Flutter:Android Studio中的内联测试范围 [英] Flutter: Inline Test Coverage in Android Studio

查看:93
本文介绍了Flutter:Android Studio中的内联测试范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter使用Android Studio 3.3.1(在Window和Ubuntu上)开发一个开源Android应用程序。来源可在 Github 上找到。



该项目有一个生成覆盖率数据的测试文件,可以使用诸如 Coveralls 的工具进行查看一个>。这向我表明LCOV数据包含有意义的数据。



我想使用内联代码覆盖率查看,类似于其他Jetbrains工具。 波动测试类别下的运行配置可以正确识别我的测试,并且可以正常运行它们。



但是,覆盖运行选项被禁用。我尝试了不同的运行配置,例如Android JUnit,但无济于事。



我知道我可以手动创建coverage数据,但是我的目标是自动生成



有人知道什么运行配置(如果有的话)可以实现此目标吗?



作为旁注,我最近将Codemagic用作我的CI工具,因此Coveralls上的coverage数据已过时,但LCOV数据有意义的观点仍然成立。我也在Intellij中尝试了类似的设置,但结果与Android Studio相同。

解决方案

我认为不支持适用于Flutter项目。



我在另一个纯Dart程序包中拥有所有非UI代码,我将它们添加为对Flutter项目的依赖。



对于我的项目,它还有一个优势,就是我可以与浏览器GUI(Angular Dart)共享的代码是分开的,并且不会被Flutter依赖项意外污染。会破坏Web项目。



在此项目中,按照以下步骤操作,我可以在IntellJ中获得覆盖率信息:



您需要一个 Dart命令行应用程序 IntelliJ运行配置,而不是 Dart测试, Flutter或 Flutter测试运行配置。



能够使用 Dart命令行应用程序运行配置运行测试,则可能需要安装独立的Dart SDK,然后在偏好设置>语言和应用程序中选择它。框架> Dart> Dart SDK路径。



要运行具有覆盖率的所有测试而不是单个文件,您需要



这样的文件

test / all.dart

  // ignore_for_file:await_only_futures 

import'dart:async';

导入 client / controller / app_controller_test.dart为i0;
导入 client / controller / authentication_controller_test.dart为i1;
导入 client / controller / backoffice / backoffice_controller_test.dart为i2;
导入 client / controller / backoffice / image_reference_controller_test.dart
作为i3;
...

Future< void> main()异步{
i0.main();
i1.main();
i2.main();
...
}

每个测试文件都有一个条目。 / p>

我使用如下所示的Grinder任务自动生成该文件

 导入'package:path / path.dart'作为路径; 
...
///生成一个执行所有测试的Dart文件。
/// Dart代码覆盖率报告仍然需要这样做。
@Task(’generate test / all.dart’)
Future< void> prepareCoverage()异步{
final testDir = Directory( test);
最终上下文= path.Context(style:path.Style.posix);
final testFiles = testDir
.listSync(递归: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();
最终内容= StringBuffer(’’
// ignore_for_file:await_only_futures

import‘dart:async’;

’’);
最终执行= StringBuffer();
for(var i = 0; i< testFiles.length; i ++){
final testFile = testFiles [i];
content.writeln(导入 $ testFile为i $ i;);
executes.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'),脚本:'format') ;
}


I'm developing an open source Android app in Flutter, using Android Studio 3.3.1 (on Window and Ubuntu). The source is available on Github.

The project has a test file that generates coverage data, which can be viewed with tools such as Coveralls. This indicates to me that the LCOV data contains meaningful data.

I want to use inline Code Coverage viewing, similar to the other Jetbrains tools. The run configuration under the 'Flutter Test' category correctly recoginezes my tests, and is able to run them properly.

However, the 'Run with Coverage' option is disabled. I tried different run configurations such as Android JUnit to no avail.

I am aware that I can manually create the coverage data, but my goal is to automate the generation of the coverage data, and showing the coverage inline (just like Coveralls does).

Does anyone know what run configuration, if any, accomplishes this goal?

As a side note, I recently switched to Codemagic as my CI tool so the coverage data on Coveralls is outdated, but the point that the LCOV data is meaningful still holds. I also tried similar setups in Intellij, but the result is the same as Android Studio.

解决方案

I don't think is supported for Flutter projects yet.

I have all non-UI code in another pure Dart package that I add as dependency to the Flutter project.

For my project this also has the advantage that code that I can share with the browser GUI (Angular Dart) is separated and can't accidentally be polluted with Flutter dependencies that would break the web project.

In this project I can get coverage information in IntellJ when I follow these steps:

You need a "Dart Command Line App" IntelliJ run configuration instead of a "Dart Test", "Flutter" or "Flutter Test" run configuration.

To be able to run tests with a "Dart Command Line App" run configuration you probably need the standalone Dart SDK installed and select it in Preferences > Languages & Frameworks > Dart > Dart SDK path.

To run all tests with coverage instead of individual files you need a file like

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();
...
} 

with an entry for each test file.

I use a Grinder task like below to generate that file automatically

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');
}

这篇关于Flutter:Android Studio中的内联测试范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆