Flutter 测试的代码覆盖率数据如何展示?

How can the code coverage data from Flutter tests be displayed?

我正在使用 Android Studio 作为我的 IDE 开发 Flutter 应用程序。我正在尝试编写测试并检查代码覆盖率,但我不知道如何查看 IDE 或任何其他应用程序中的数据。

运行flutter test --coverage,覆盖率报告似乎生成到文件 /coverage/lcov.info 中。该文件看起来像这样:

SF:lib\data\Customer.g.dart
DA:9,2
DA:10,2
DA:11,2
DA:12,2
DA:13,2
DA:20,0
DA:21,0
DA:22,0
DA:23,0
DA:24,0
...

查看该文件,它似乎包含我的项目文件列表以及逐行覆盖数据。有没有办法在 Android Studio 中查看此信息?

可以通过Atom editor查看flutter生成的代码覆盖率。
你只需要安装 Dart 和 lcov-info 包。

然后加载项目文件夹并按 Ctrl+Alt+c,将显示覆盖范围,其中包含整个项目覆盖范围的摘要以及特定行突出显示。

Android studio 似乎还没有任何插件可以做到这一点。

Android Studio

现已提供覆盖率报告

您还可以安装 lcov 并将 lcov.info 文件转换为 HTML 页面,然后在带有排序选项的浏览器中查看结果。

1。安装

1.1。在 Ubuntu

安装
sudo apt-get update -qq -y
sudo apt-get install lcov -y

1.2。在 Mac

安装
brew install lcov

2。 运行 测试,生成覆盖率文件并转换为 HTML

flutter test --coverage
genhtml coverage/lcov.info -o coverage/html

注意 这样您就可以将它添加到 circleci 工件和 coveralls 中。

2021 年 9 月 18 日更新:

2020 年 5 月 9 日更新:

原来你可以 运行 flutter test --coverage,然后在同一个终端会话中 运行 bash <(curl -s https://codecov.io/bash) -t token 令牌应该是你从 [=13= 获得的存储库令牌].该命令应自动查找并上传覆盖率数据,并将在您的 CodeCov 仪表板上可见。所以你不需要 Bitrise。

原文:

我一直在使用 Bitrise for continuous integration on my flutter project and there is an easy way to send your reports to CodeCov 然后在那里可视化它。这需要你获得一些关于如何设置和使用 Bitrise 的知识,但它的很多知识都是自动的,所以不要担心,如果你是一个小团队,你应该可以使用免费套餐。以下是让 CodeCov 工作的关键点。

  1. 确保将 --coverage 变量添加到 Flutter 测试工作流程。

  1. 将来自 CodeCov 的令牌添加为密钥,您需要注册 CodeCov 和 link 您的存储库才能接收令牌。

  1. 添加 CodeCov 工作流和 select CODECOV_TOKEN 键。

之后,您应该能够启动构建,如果成功,您应该会在 CodeCov 看到您的仪表板更新。

您可以按照此 link SonarQube plugin for Flutter / Dart.

将 SonarQube 与 Flutter 的附加插件一起使用

我已经在 docker 上用免费版的 SonarQube 试过了,如果你配置正确,你只需要 运行 在 Android Studio 终端上执行以下命令:

# Download dependencies 
flutter pub get 
# Run tests with User feedback (in case some test are failing)
flutter test
# Run tests without user feedback regeneration tests.output and coverage/lcov.info
flutter test --machine --coverage > tests.output 

# Run the analysis and publish to the SonarQube server
sonar-scanner

这里是报表样例,你可以深入到代码行。

因此,实际答案是否定的,您目前无法在 Android Studio(或 IntelliJ IDEA)中查看覆盖率报告。

与 JavaScript/TypeScript 和 Java 以及可能 Python 不同,IntelliJ IDE(以及 Android Studio)没有集成 IDE 支持在编辑器中显示 Flutter 代码的测试覆盖率。这是一种耻辱,因为能够在编辑器的源代码中看到突出显示的未经测试的代码分支和行是一件美妙的事情。不知道为什么这个插件还不存在,因为它对其他语言有很好的支持,并且生成了一个标准的 lcov.info 文件。

IntelliJ 中有一个捆绑的代码覆盖工具 window 应该允许您以 tree/table 向下钻取格式浏览 lcov.info 文件,但它没有似乎无法使用 flutter (flutter test --coverage) 生成的覆盖率报告。我认为这可能是 lcov.info 和我的多模块应用程序结构中的相对路径,但我尝试手动编辑 lcov.info 中的文件路径,但我没有运气显示统计信息。

这就是您想要的 运行 在 macOS 上的浏览​​器中查看测试覆盖率

flutter test --coverage
genhtml coverage/lcov.info -o coverage/html
open coverage/html/index.html

我刚刚开发了一个简单的 dart 包 (test_cov_console), so you can run it directly from Android Studio terminal. The tool would read the lcov.info that was generated by flutter test --coverage. Find this link for source code.

您可以全局安装该库,因此它不会更改您当前的项目:

    flutter pub global activate test_cov_console

和运行它:

    flutter pub global run test_cov_console

这是输出示例:

    flutter pub run test_cov_console
---------------------------------------------|---------|---------|---------|-------------------|
File                                         |% Branch | % Funcs | % Lines | Uncovered Line #s |
---------------------------------------------|---------|---------|---------|-------------------|
lib/src/                                     |         |         |         |                   |
 print_cov.dart                              |  100.00 |  100.00 |   88.37 |...,149,205,206,207|
 print_cov_constants.dart                    |    0.00 |    0.00 |    0.00 |    no unit testing|
lib/                                         |         |         |         |                   |
 test_cov_console.dart                       |    0.00 |    0.00 |    0.00 |    no unit testing|
---------------------------------------------|---------|---------|---------|-------------------|
 All files with unit testing                 |  100.00 |  100.00 |   88.37 |                   |
---------------------------------------------|---------|---------|---------|-------------------|

输出可以保存到 CSV 文件:

flutter pub run test_cov_console -c --output=coverage/test_coverage.csv

CSV 输出文件示例:

File,% Branch,% Funcs,% Lines,Uncovered Line #s
lib/,,,,
test_cov_console.dart,0.00,0.00,0.00,no unit testing
lib/src/,,,,
parser.dart,100.00,100.00,97.22,"97"
parser_constants.dart,100.00,100.00,100.00,""
print_cov.dart,100.00,100.00,82.91,"29,49,51,52,171,174,177,180,183,184,185,186,187,188,279,324,325,387,388,389,390,391,392,393,394,395,398"
print_cov_constants.dart,0.00,0.00,0.00,no unit testing
All files with unit testing,100.00,100.00,86.07,""

Flutter Enhancement Suite 正是这样做的。这是一个生成覆盖率报告的 Android Studio/IntelliJ 插件。 它显示了每个文件的覆盖范围,还突出显示了覆盖的行(行号旁边的 red/green 条):

  1. install the plugin 来自插件选项 (Preferences > Plugins > Marketplace 选项卡 > 搜索 Flutter Enhancement Suite).

  2. 创建一个新的运行 配置 用于测试覆盖率

(运行 > 编辑配置 > 单击加号按钮添加新配置 > 在下拉列表中选择 Flutter Test)

  1. 为您的配置命名(例如“所有测试”),设置范围和包含您的测试的文件或目录。

  2. 运行 您的测试覆盖了顶部菜单。

随着 Flutter 2.5 的发布,您现在可以在 IntelliJ 和 Android Studio 中查看测试覆盖率。
See this post

In addition, the latest IJ/AS plugin for Flutter allows you to see the coverage information for both unit test and integration test runs. You can access this from the toolbar button next to the “Debug” button:

Android Studio 和 IntelliJ: