测试驱动开发(Test-Driven Development,TDD) 是一种实践,你在编写代码之前先写测试——遵循一个短的、重复的循环:写一个失败的测试,编写代码使其通过,然后重构。它通过测试驱动设计,确保代码可测试且经过充分测试。
TDD 循环:Red、Green、Refactor
text
1. RED → write a FAILING test for the behavior you want (it fails — the code doesn't exist yet)
2. GREEN → write the MINIMUM code to make the test PASS (just enough, don't over-build)
3. REFACTOR → improve/clean up the code (and tests) while keeping tests GREEN
→ Repeat this short cycle for each small piece of behavior.
text
Write test (RED) → write code (GREEN) → clean up (REFACTOR) → repeat
示例流程
js
(, ((, )).());
() { a + b; }
