dart-test-fundamentals

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dart Test Fundamentals

Dart测试基础

When to use this skill

何时使用本技能

Use this skill when:
  • Writing new test files.
  • structuring test suites with
    group
    .
  • Configuring test execution via
    dart_test.yaml
    .
  • Understanding test lifecycle methods.
当你遇到以下场景时可使用本技能:
  • 编写新测试文件
  • 使用
    group
    构建测试套件
  • 通过
    dart_test.yaml
    配置测试执行规则
  • 需要了解测试生命周期方法

Core Concepts

核心概念

1. Test Structure (
test
and
group
)

1. 测试结构(
test
group

  • test
    : The fundamental unit of testing.
    dart
    test('description', () {
      // assertions
    });
  • group
    : Used to organize tests into logical blocks.
    • Groups can be nested.
    • Descriptions are concatenated (e.g., "Group Description Test Description").
    • Helps scope
      setUp
      and
      tearDown
      calls.
    • Naming: Use
      PascalCase
      for groups that correspond to a class name (e.g.,
      group('MyClient', ...)
      ).
    • Avoid Single Groups: Do not wrap all tests in a file with a single
      group
      call if it's the only one.
  • Naming Tests:
    • Avoid redundant "test" prefixes.
    • Include the expected behavior or outcome in the description (e.g.,
      'throws StateError'
      or
      'adds API key to URL'
      ).
    • Descriptions should read well when concatenated with their group name.
  • Named Parameters Placement:
    • For
      test
      and
      group
      calls, place named parameters (e.g.,
      testOn
      ,
      timeout
      ,
      skip
      ) immediately after the description string, before the callback closure. This improves readability by keeping the test logic last.
      dart
      test('description', testOn: 'vm', () {
        // assertions
      });
  • test
    :测试的基本单元
    dart
    test('description', () {
      // 断言逻辑
    });
  • group
    :用于将测试组织为逻辑块
    • 支持嵌套分组
    • 描述会自动拼接(例如:"组描述 测试描述")
    • 可用于限定
      setUp
      tearDown
      的作用范围
    • 命名规则:对应类名的分组使用
      PascalCase
      命名(例如:
      group('MyClient', ...)
    • 避免单个冗余分组:如果文件内只有一个分组,不要用单个
      group
      包裹所有测试
  • 测试命名规范
    • 避免添加冗余的"test"前缀
    • 描述中要包含预期行为或结果(例如:
      'throws StateError'
      'adds API key to URL'
    • 描述与所属组名拼接后语义通顺
  • 命名参数放置规则
    • 调用
      test
      group
      时,将命名参数(例如
      testOn
      timeout
      skip
      )放在描述字符串之后、回调闭包之前,将测试逻辑放在最后以提升可读性
      dart
      test('description', testOn: 'vm', () {
        // 断言逻辑
      });

2. Lifecycle Methods (
setUp
,
tearDown
)

2. 生命周期方法(
setUp
tearDown

  • setUp
    : Runs before every
    test
    in the current
    group
    (and nested groups).
  • tearDown
    : Runs after every
    test
    in the current
    group
    .
  • setUpAll
    : Runs once before any test in the group.
  • tearDownAll
    : Runs once after all tests in the group.
Best Practice:
  • Use
    setUp
    for resetting state to ensure test isolation.
  • Avoid sharing mutable state between tests without resetting it.
  • setUp
    :在当前
    group
    (含嵌套分组)的每个
    test
    执行运行
  • tearDown
    :在当前
    group
    的每个
    test
    执行运行
  • setUpAll
    :在分组内所有测试开始前仅运行一次
  • tearDownAll
    :在分组内所有测试结束后仅运行一次
最佳实践:
  • 使用
    setUp
    重置状态,保证测试间隔离
  • 避免在测试间共享可变状态且不做重置

3. Configuration (
dart_test.yaml
)

3. 配置(
dart_test.yaml

The
dart_test.yaml
file configures the test runner. Common configurations include:
dart_test.yaml
文件用于配置测试运行器,常见配置项包括:

Platforms

运行平台

Define where tests run (vm, chrome, node).
yaml
platforms:
  - vm
  - chrome
定义测试的执行环境(vm、chrome、node)
yaml
platforms:
  - vm
  - chrome

Tags

标签

Categorize tests to run specific subsets.
yaml
tags:
  integration:
    timeout: 2x
Usage in code:
dart
(['integration'])
import 'package:test/test.dart';
Running tags:
dart test --tags integration
对测试分类,支持运行指定子集
yaml
tags:
  integration:
    timeout: 2x
代码中使用方式:
dart
(['integration'])
import 'package:test/test.dart';
运行指定标签的测试:
dart test --tags integration

Timeouts

超时时间

Set default timeouts for tests.
yaml
timeouts:
  2x # Double the default timeout
设置测试的默认超时阈值
yaml
timeouts:
  2x # 将默认超时时间翻倍

4. File Naming

4. 文件命名

  • Test files must end in
    _test.dart
    to be picked up by the test runner.
  • Place tests in the
    test/
    directory.
  • 测试文件必须
    _test.dart
    结尾,才会被测试运行器识别
  • 测试文件需放在
    test/
    目录下

Common commands

常用命令

  • dart test
    : Run all tests.
  • dart test test/path/to/file_test.dart
    : Run a specific file.
  • dart test --name "substring"
    : Run tests matching a description.
  • dart test
    :运行所有测试
  • dart test test/path/to/file_test.dart
    :运行指定文件的测试
  • dart test --name "substring"
    :运行描述匹配指定字符串的测试

Related Skills

相关技能

dart-test-fundamentals
is the core skill for structuring and configuring tests. For writing assertions within those tests, refer to:
  • dart-matcher-best-practices
    : Use this if the project sticks with the traditional
    package:matcher
    (
    expect
    calls).
  • dart-checks-migration
    : Use this if the project is migrating to the modern
    package:checks
    (
    check
    calls).
dart-test-fundamentals
是构建和配置测试的核心技能,如需了解测试内的断言写法,可参考:
  • dart-matcher-best-practices
    :适用于项目使用传统
    package:matcher
    expect
    调用)的场景
  • dart-checks-migration
    :适用于项目迁移到现代
    package:checks
    check
    调用)的场景