单元测试通过隔离验证代码的个别单元(函数、方法、类),确保每个小片段单独正确运行。单元测试速度快、目标明确,是良好测试策略的基础。
什么是单元测试
text
A UNIT test verifies the smallest testable piece (a function/method) in ISOLATION:
→ test ONE thing; give inputs, check the output/behavior
→ ISOLATED from dependencies (database, network, other units) — mock/stub those
→ FAST (no real I/O), focused, deterministic
示例
js
() { a + b; }
(, {
((, )).();
((-, )).();
});
(, {
cart = ();
cart.({ : });
cart.({ : });
(cart.).();
});
