|
| 1 | +# Test-driven Development |
| 2 | + |
| 3 | +**Test-driven development** (TDD) is a process where developers write the tests first, |
| 4 | +**before** writing the code. As we saw in the last section, the tests alone can |
| 5 | +be sufficient for specifying the behavior of the function. |
| 6 | + |
| 7 | +Even to modify existing code, a test would be written first under this |
| 8 | +philosophy. For example, if you had an `if` statement in your code already, but |
| 9 | +you wanted to add an `else` branch, under TDD, you would first write a test for |
| 10 | +that `else` branch, and then write the actual code. As mentioned before, at the |
| 11 | +highest levels of testing, a mistake in any line of code should be caught, and |
| 12 | +TDD facilitates this. |
| 13 | + |
| 14 | +## Why TDD? |
| 15 | + |
| 16 | +First, TDD requires the developer to first think |
| 17 | +about the behavior of the tested code and how it will be used, rather than |
| 18 | +rushing head-first into writing the code. Perhaps you may have experienced |
| 19 | +writing code and realizing later that the code you wrote was not quite what you |
| 20 | +needed. When you think carefully first about the behavior of the code as in TDD, |
| 21 | +this situation is often avoided. |
| 22 | + |
| 23 | +Second, as developers must write tests with every code, TDD requires developers |
| 24 | +to write tests and puts developers in the habit of writing tests, which is |
| 25 | +generally healthy for the robustness of a codebase. If TDD was not present, |
| 26 | +depending on the company, developers may be less strict about writing tests. |
| 27 | + |
| 28 | +## Is TDD used in the real world? |
| 29 | +Many companies do adopt TDD, although in practice, this is a minority of |
| 30 | +companies. TDD is far from a perfect philosophy: it does not guarantee good test |
| 31 | +coverage and it may even encourage developers to write sloppier tests so that |
| 32 | +they can simply start coding faster. |
| 33 | + |
| 34 | +However, as with all things |
| 35 | +in life, understanding the perspective of TDD is still extremely useful. |
| 36 | +But as all things in life, learning different perspectives is useful. |
| 37 | +TDD is on the very rigorous end of the role of testing in |
| 38 | +development. In reality, you will probably end up somewhere in between -- |
| 39 | +perhaps sometimes you will think about the tests first, or you will think in |
| 40 | +your head about what will be tested, even though you may not be required to |
| 41 | +actually write the tests as you would in TDD. |
| 42 | + |
| 43 | +The reason that understanding or trying test-driven development is helpful is |
| 44 | +because even if you do not work at a company that adopts test-driven |
| 45 | +development, it will encourage you to think about success and failure cases |
| 46 | +while you write your code. **This is a crucial part of the programmer's |
| 47 | +mindset.** As you become a more experienced programmer, you will try to predict |
| 48 | +issues; test-driven development aids greatly with this mindset, since it |
| 49 | +encourages you to write the test and think about these scenarios before writing |
| 50 | +the code. |
0 commit comments