排列-Act-Assert (AAA)
一句话解释
AAA 是测试模式: 排列 (设置数据), Act (call code), Assert (检查 result). 它 improves 测试 readability.
用大白话说
排列-Act-Assert (AAA) 是简单模式 for structuring 测试. 它划分每个测试到 three 清晰小节:
- Arrange: 设置一切测试需要 — 创建测试数据, initialize 变量, 配置 mocks. 这个是 'given' 部分.
- Act: 执行 code 是 tested — call 函数使用排列输入. 这个是 '当' 部分. 通常只是 one 行.
- Assert: 验证 result — 检查那个输出, 边副作用, or 状态更改 match expectations. 这个是 '然后' 部分.
The 模式 improves readability 通过制作结构明显. 当你 读取测试, 你能 清晰地 see 什么's 设置, 什么's 操作, and 什么's 是验证. 一些 teams 偶数添加空白行 or comments 之间小节.
示例
javascript
test('calculate total with discount', () => {
// Arrange
const items = [10, 20, 30];
const discount = 0.1;
// Act
const total = calculateTotal(items, discount);
// Assert
expect(total).toBe(54); // 60 - 10%
});three 小节是 清晰地 separated: 设置, execution, verification.
相关术语
相关资源
相关课程
- 测试最佳实践
编写 maintainable 测试.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。