test-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWhen to use this skill
何时使用此技能
In a Dart or Flutter project.
When a user asks to "enforce test best practices" or similar.
When modifying or creating test files.
在Dart或Flutter项目中。
当用户要求“推行测试最佳实践”或类似需求时。
在修改或创建测试文件时。
Workflow
工作流程
- Search: Use the grep commands below to identify candidates.
- Analyze: Check if the code violates the best practices defined below.
- Apply: Refactor the code to use the recommended matchers.
- Verify: Run tests () to ensure no regressions.
dart test
- 搜索:使用以下grep命令识别候选代码。
- 分析:检查代码是否违反以下定义的最佳实践。
- 应用:重构代码以使用推荐的匹配器。
- 验证:运行测试()确保没有回归问题。
dart test
Search Strategies
搜索策略
- :
.lengthgrep -r "\.length,\s*equals\(" test/ - Boolean properties:
grep -rE "expect\(.*\.(is(Empty|NotEmpty)),\s*(isTrue|true|isFalse|false)" test/ - Manual loops: (manual review required)
grep -r "for (var .* in .*)" test/
- :
.lengthgrep -r "\.length,\s*equals\(" test/ - 布尔属性:
grep -rE "expect\(.*\.(is(Empty|NotEmpty)),\s*(isTrue|true|isFalse|false)" test/ - 手动循环:(需手动审查)
grep -r "for (var .* in .*)" test/
Best Practice Patterns
最佳实践模式
Collections
集合
Use hasLength
hasLength使用hasLength
hasLengthPrefer over .
Applies to: , , .
expect(list, hasLength(n))expect(list.length, n)IterableMapString优先使用而非。
适用范围:、、。
expect(list, hasLength(n))expect(list.length, n)IterableMapStringUse isEmpty
/ isNotEmpty
isEmptyisNotEmpty使用isEmpty
/ isNotEmpty
isEmptyisNotEmptyPrefer over .
Prefer over or
.
Applies to: , , .
expect(list, isEmpty)expect(list.isEmpty, true)expect(list, isNotEmpty)expect(list.isNotEmpty, true)expect(list, isNot(isEmpty))IterableMapString优先使用而非。
优先使用而非或
。
适用范围:、、。
expect(list, isEmpty)expect(list.isEmpty, true)expect(list, isNotEmpty)expect(list.isNotEmpty, true)expect(list, isNot(isEmpty))IterableMapStringDeclarative Verification
声明式验证
Prefer over manual loops with assertions.
expect(list, everyElement(matcher))优先使用而非带断言的手动循环。
expect(list, everyElement(matcher))List Equality
列表相等性
Prefer over manual loops checking elements by index.
provides readable diffs for list mismatches.
expect(actualList, expectedList)package:test优先使用而非按索引检查元素的手动循环。
会为列表不匹配情况提供易读的差异信息。
expect(actualList, expectedList)package:testMaps
映射
Use containsPair
containsPair使用containsPair
containsPairPrefer over .
expect(map, containsPair(key, value))expect(map[key], value)Note: If verifying a key is missing, use .
expect(map, isNot(contains(key)))优先使用而非。
expect(map, containsPair(key, value))expect(map[key], value)注意:若要验证某个键不存在,使用。
expect(map, isNot(contains(key)))Strict Equality
严格相等性
Prefer over multiple calls when the
full map is known.
expect(map, {'k': 'v'})containsPair当已知完整映射内容时,优先使用而非多次调用。
expect(map, {'k': 'v'})containsPairTypes & Objects
类型与对象
Declarative Type Checks
声明式类型检查
Prefer over .
expect(obj, isA<T>())expect(obj is T, isTrue)优先使用而非。
expect(obj, isA<T>())expect(obj is T, isTrue)Grouped Assertions
分组断言
Prefer chaining on for multiple property checks.
havingisA<T>dart
expect(obj, isA<MyType>()
.having((o) => o.prop1, 'prop1', a)
.having((o) => o.prop2, 'prop2', b));优先在上链式调用来进行多属性检查。
isA<T>havingdart
expect(obj, isA<MyType>()
.having((o) => o.prop1, 'prop1', a)
.having((o) => o.prop2, 'prop2', b));Constraints
约束条件
- Verify Types: Ensure subject is strictly /
Iterablebefore applying collection matchers. Some custom classes (e.g.Map) may havePriorityQueuebut don't implement.length.Iterable - Do NOT migrate to package:checks: Unless explicitly requested. This
skill focuses on matchers.
package:test - Preserve Behavior: Ensure refactorings do not change strictness.
- 验证类型:在应用集合匹配器前,确保目标对象严格为/
Iterable。部分自定义类(如Map)可能有PriorityQueue但未实现.length。Iterable - 请勿迁移至package:checks:除非明确要求。此技能专注于匹配器。
package:test - 保留行为:确保重构不会改变严格性。