|
| 1 | +# Why is testing important? |
| 2 | + |
| 3 | +You may think it is sufficient simply to manually test your code. |
| 4 | + |
| 5 | +There are a few reasons testing is very important when it comes to a growing |
| 6 | +codebase. You should not imagine the situation where you are working on a |
| 7 | +project with one or two people, but a situation where you are working with a |
| 8 | +larger team and with code that the company will use the code long after you leave. You |
| 9 | +may not be at the company five years later, or you may forget what the code does |
| 10 | +in six months. |
| 11 | + |
| 12 | +## Correctness |
| 13 | +Most obviously, a test is used to verify the correctness of the code. If you |
| 14 | +have ever changed your code then had to test three or more cases manually, then |
| 15 | +you are familiar with the fact that it is quite cumbersome to repeatedly check |
| 16 | +manually. Not only that, it's very easy to forget to check some case when a |
| 17 | +human is doing it by hand. |
| 18 | + |
| 19 | +In contract, when you write a test, you simply need to run a command in order to |
| 20 | +initiate the tests again, rather than repeating some steps to ensure that your |
| 21 | +code is working. |
| 22 | + |
| 23 | +If you work at a company with code review, consider the perspective of the |
| 24 | +person reviewing your code: how do they know your code doesn't have bugs? How do |
| 25 | +they know it works correctly? If your code is submitted along with tests, the |
| 26 | +reviewer can be aware of exactly what you have or haven't verified about your |
| 27 | +code. |
| 28 | + |
| 29 | +## Ease of change |
| 30 | +Have you ever |
| 31 | +felt scared to change code, because you weren't sure if it would still work for |
| 32 | +all the cases after you changed it? With a good test suite, this fear generally |
| 33 | +doesn't exist. Even if the code is rewritten but the same tests pass, indicating |
| 34 | +that the new code has the same behavior, one can feel a lot more secure about |
| 35 | +rewriting the code. |
| 36 | + |
| 37 | +Having good tests allows code to be changed in a more robust fashion. When the |
| 38 | +same tests pass, a developer can be more sure (perhaps not completely sure, |
| 39 | +depending on the tests) that changing the code did not break anything. |
| 40 | +Additionally, it allows people who are not familiar with the code (say, your |
| 41 | +teammates who work on related code, or someone who is responsible for your code |
| 42 | +five years later) to more easily work with the code. |
| 43 | + |
| 44 | +## Documentation |
| 45 | +A well-written test suite serves as documentation. Again, imagine you have just |
| 46 | +joined a company, and you don't know what a function or component does. In fact, if you want to |
| 47 | +understand what a piece of code does, it's often more productive to go read the |
| 48 | +tests first, rather than the code itself. |
| 49 | + |
| 50 | +In the next section, we'll look an |
| 51 | +example of a test, which will help illustrate how the tests themselves can be |
| 52 | +used to document the code. |
| 53 | + |
0 commit comments