flutter-test-discovery

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Flutter test discovery

Flutter测试文件识别

Ensure every intended test file actually runs. The runner is a names filter: only files matching
*_test.dart
are discovered by
flutter test
with no arguments.
确保每个目标测试文件都能实际运行。测试运行器是一个名称过滤器:不带参数的
flutter test
只会识别匹配
*_test.dart
的文件。

Steps

步骤

  1. When the test counter looks low, list the test directory and check names:
    ls test/
    — any file ending in
    _tests.dart
    (plural) will be silently skipped. Completion: you can name every file and whether it ends in
    _test.dart
    .
  2. Rename non-conforming files so the suite is discovered:
    mv test/acceptance_tests.dart test/acceptance_test.dart
    .
  3. Re-run the full suite and compare the counter to the expected total. Completion: counter matches; the acceptance file's tests appear in the summary.
  4. Run a single case while debugging with
    flutter test test/<file>_test.dart --plain-name 'TEST-003'
    .
  1. 当测试计数看起来偏低时,列出测试目录并检查文件名:
    ls test/
    — 任何以
    _tests.dart
    (复数形式)结尾的文件都会被静默跳过。 完成标志:你可以列出所有文件及其是否以
    _test.dart
    结尾。
  2. 重命名不符合规则的文件,使其能被测试套件识别:
    mv test/acceptance_tests.dart test/acceptance_test.dart
    。
  3. 重新运行完整测试套件,并将计数与预期总数对比。完成标志:计数匹配;验收测试文件中的测试会出现在汇总结果中。
  4. 调试时运行单个测试用例,使用命令
    flutter test test/<file>_test.dart --plain-name 'TEST-003'
    。

Reference

参考信息

  • Evidence from this project:
    flutter test
    reported
    +14
    while 28 tests existed; the 14 acceptance tests in
    acceptance_tests.dart
    were silently excluded until renamed to
    acceptance_test.dart
    .
  • Per-file runs (
    flutter test test/x.dart
    ) work with any name — the discovery gap only bites the full-suite run.
  • If a full-suite run seems too fast, suspect a name, not the tests.
  • 项目实例:
    flutter test
    报告执行了
    +14
    个测试,但实际存在28个测试;
    acceptance_tests.dart
    中的14个验收测试在重命名为
    acceptance_test.dart
    之前一直被静默排除。
  • 单个文件运行(
    flutter test test/x.dart
    )支持任意文件名——只有在运行完整测试套件时才会出现文件识别遗漏的问题。
  • 如果完整测试套件运行速度异常快,应怀疑是文件名问题,而非测试本身的问题。