dart-test-fundamentals
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDart 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
)
testgroup1. 测试结构(test
与group
)
testgroup-
: The fundamental unit of testing.
testdarttest('description', () { // assertions }); -
: Used to organize tests into logical blocks.
group- Groups can be nested.
- Descriptions are concatenated (e.g., "Group Description Test Description").
- Helps scope and
setUpcalls.tearDown - Naming: Use for groups that correspond to a class name (e.g.,
PascalCase).group('MyClient', ...) - Avoid Single Groups: Do not wrap all tests in a file with a single
call if it's the only one.
group
-
Naming Tests:
- Avoid redundant "test" prefixes.
- Include the expected behavior or outcome in the description (e.g.,
or
'throws StateError').'adds API key to URL' - Descriptions should read well when concatenated with their group name.
-
Named Parameters Placement:
- For and
testcalls, place named parameters (e.g.,group,testOn,timeout) immediately after the description string, before the callback closure. This improves readability by keeping the test logic last.skipdarttest('description', testOn: 'vm', () { // assertions });
- For
-
:测试的基本单元
testdarttest('description', () { // 断言逻辑 }); -
:用于将测试组织为逻辑块
group- 支持嵌套分组
- 描述会自动拼接(例如:"组描述 测试描述")
- 可用于限定和
setUp的作用范围tearDown - 命名规则:对应类名的分组使用命名(例如:
PascalCase)group('MyClient', ...) - 避免单个冗余分组:如果文件内只有一个分组,不要用单个包裹所有测试
group
-
测试命名规范:
- 避免添加冗余的"test"前缀
- 描述中要包含预期行为或结果(例如:或
'throws StateError')'adds API key to URL' - 描述与所属组名拼接后语义通顺
-
命名参数放置规则:
- 调用和
test时,将命名参数(例如group、testOn、timeout)放在描述字符串之后、回调闭包之前,将测试逻辑放在最后以提升可读性skipdarttest('description', testOn: 'vm', () { // 断言逻辑 });
- 调用
2. Lifecycle Methods (setUp
, tearDown
)
setUptearDown2. 生命周期方法(setUp
、tearDown
)
setUptearDown- : Runs before every
setUpin the currenttest(and nested groups).group - : Runs after every
tearDownin the currenttest.group - : Runs once before any test in the group.
setUpAll - : Runs once after all tests in the group.
tearDownAll
Best Practice:
- Use for resetting state to ensure test isolation.
setUp - Avoid sharing mutable state between tests without resetting it.
- :在当前
setUp(含嵌套分组)的每个group执行前运行test - :在当前
tearDown的每个group执行后运行test - :在分组内所有测试开始前仅运行一次
setUpAll - :在分组内所有测试结束后仅运行一次
tearDownAll
最佳实践:
- 使用重置状态,保证测试间隔离
setUp - 避免在测试间共享可变状态且不做重置
3. Configuration (dart_test.yaml
)
dart_test.yaml3. 配置(dart_test.yaml
)
dart_test.yamlThe file configures the test runner. Common configurations
include:
dart_test.yamldart_test.yamlPlatforms
运行平台
Define where tests run (vm, chrome, node).
yaml
platforms:
- vm
- chrome定义测试的执行环境(vm、chrome、node)
yaml
platforms:
- vm
- chromeTags
标签
Categorize tests to run specific subsets.
yaml
tags:
integration:
timeout: 2xUsage 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 integrationTimeouts
超时时间
Set default timeouts for tests.
yaml
timeouts:
2x # Double the default timeout设置测试的默认超时阈值
yaml
timeouts:
2x # 将默认超时时间翻倍4. File Naming
4. 文件命名
- Test files must end in to be picked up by the test runner.
_test.dart - Place tests in the directory.
test/
- 测试文件必须以结尾,才会被测试运行器识别
_test.dart - 测试文件需放在目录下
test/
Common commands
常用命令
- : Run all tests.
dart test - : Run a specific file.
dart test test/path/to/file_test.dart - : Run tests matching a description.
dart test --name "substring"
- :运行所有测试
dart test - :运行指定文件的测试
dart test test/path/to/file_test.dart - :运行描述匹配指定字符串的测试
dart test --name "substring"
Related Skills
相关技能
dart-test-fundamentals- : Use this if the project sticks with the traditional
dart-matcher-best-practices(package:matchercalls).expect - : Use this if the project is migrating to the modern
dart-checks-migration(package:checkscalls).check
dart-test-fundamentals- :适用于项目使用传统
dart-matcher-best-practices(package:matcher调用)的场景expect - :适用于项目迁移到现代
dart-checks-migration(package:checks调用)的场景check