Content deleted Content added
No edit summary |
Updated TDD cycle to add "create a list of all tests" as described by Kent Beck |
||
Line 20:
== Test-driven development cycle ==
[[File:TDD Global Lifecycle.png|thumb|A graphical representation of the test-driven development lifecycle]]
The following sequence is based on the book ''Test-Driven Development by Example''
;1. Create a list of tests for the new feature
:The
;2. Add one test from the list
;2. Run all tests. The new test ''should fail'' for expected reasons▼
:Write an automated test that passes if the variant in the new behavior is met.
:This shows that new code is actually needed for the desired feature. It validates that the [[test harness]] is working correctly. It rules out the possibility that the new test is flawed and will always pass.
;
:Inelegant or [[hard code]] is acceptable, as long as it passes the test. The code will be honed anyway in Step
;
:If any fail, the new code must be revised until they pass. This ensures the new code meets the [[Software requirements|test requirements]] and does not break existing features.
;
:Code is [[Code refactoring|refactored]] for [[Code readability|readability]] and maintainability. In particular, hard-coded test data should be removed. Running the test suite after each refactor helps ensure that no existing functionality is broken.
:*Examples of refactoring:
Line 38 ⟶ 40:
:** splitting methods into smaller pieces
:** re-arranging [[Inheritance (object-oriented programming)|inheritance hierarchies]]
;7. Add the next test on the list
:Repeat the process with each test on the list until all tests are implemented and passing.
;Repeat
:The cycle above is repeated for each new piece of functionality. Tests should be small and incremental, and commits made often. That way, if new code fails some tests, the programmer can simply [[undo]] or revert rather than [[debug]] excessively. When using [[Library (computing)|external libraries]], it is important not to write tests that are so small as to effectively test merely the library itself,<ref name=Newkirk /> unless there is some reason to believe that the library is buggy or not feature-rich enough to serve all the needs of the software under development.
|