简体   繁体   English

Flutter:Android Studio 中的内联测试覆盖率

[英]Flutter: Inline Test Coverage in Android Studio

I'm developing an open source Android app in Flutter, using Android Studio 3.3.1 (on Window and Ubuntu).我正在 Flutter 中开发一个开源 Android 应用程序,使用 Android Studio 3.3.1(在 Window 和 Ubuntu 上)。 The source is available on Github .源可在Github 上找到

The project has a test file that generates coverage data, which can be viewed with tools such as Coveralls .该项目有一个生成覆盖数据的测试文件,可以使用Coveralls等工具查看。 This indicates to me that the LCOV data contains meaningful data.这向我表明 LCOV 数据包含有意义的数据。

I want to use inline Code Coverage viewing, similar to the other Jetbrains tools.我想使用内联代码覆盖率查看,类似于其他 Jetbrains 工具。 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.但是,“Run with Coverage”选项被禁用。 I tried different run configurations such as Android JUnit to no avail.我尝试了不同的运行配置,例如 Android JUnit 无济于事。

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).我知道我可以手动创建覆盖率数据,但我的目标是自动生成覆盖率数据,并内联显示覆盖率(就像 Coveralls 一样)。

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.作为旁注,我最近切换到 Codemagic 作为我的 CI 工具,因此 Coveralls 上的覆盖数据已经过时,但 LCOV 数据有意义的观点仍然成立。 I also tried similar setups in Intellij, but the result is the same as Android Studio.我也在 Intellij 中尝试过类似的设置,但结果与 Android Studio 相同。

I don't think is supported for Flutter projects yet.我认为 Flutter 项目尚不支持。

I have all non-UI code in another pure Dart package that I add as dependency to the Flutter project.我在另一个纯 Dart 包中拥有所有非 UI 代码,我将其添加为 Flutter 项目的依赖项。

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.对于我的项目,这也有一个优点,即我可以与浏览器 GUI(Angular Dart)共享的代码是分开的,并且不会意外地被 Flutter 依赖项污染,这会破坏 Web 项目。

In this project I can get coverage information in IntellJ when I follow these steps:在这个项目中,当我按照以下步骤操作时,我可以在 IntellJ 中获取覆盖信息:

You need a "Dart Command Line App" IntelliJ run configuration instead of a "Dart Test", "Flutter" or "Flutter Test" run configuration.您需要一个“Dart 命令行应用程序”IntelliJ 运行配置,而不是“Dart Test”、“Flutter”或“Flutter Test”运行配置。

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.为了能够使用“Dart 命令行应用程序”运行配置运行测试,您可能需要安装独立的 Dart SDK,并在 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我使用如下所示的 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');
}

The 'Run with Coverage' option in Android Studio is enabled only for Flutter integration tests for some reason (at least for me).由于某种原因(至少对我而言),Android Studio 中的“Run with Coverage”选项仅针对 Flutter 集成测试启用。

I wrote an article that describes how to generate code coverage reports locally and on CodeCov and CoverAlls that should get you close to what you want to do.我写了一篇文章,描述了如何在本地以及 CodeCov 和 CoverAlls 上生成代码覆盖率报告,它们应该能让您接近您想要做的事情。 Includes all source code and shows live examples.包括所有源代码并显示现场示例。

It works for both flutter and dart packages.它适用于 flutter 和 dart 包。

You can find it here: https://medium.com/@nocnoc/combined-code-coverage-for-flutter-and-dart-237b9563ecf8你可以在这里找到它: https : //medium.com/@nocnoc/combined-code-coverage-for-flutter-and-dart-237b9563ecf8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM