5 Your own personal testing robot: Automated testing and test driven development
In this chapter, you will learn
- The benefits of automated testing and when to apply it
- How to do test driven development (TDD)
- Making it look like you did TDD, even when you didn’t
- Putting TDD to good use even when you aren’t using it
Why is testing important? The natural state of code is to be broken. The only way we can know if it’s broken or working properly is to test it and get feedback so that we can verify the state of our code. Broken code is not useful. Broken code is not valuable. Only testing and the subsequent feedback can prove that our code actually works.
Keep your code working (section 1.4.3) is my most fundamental rule of development. But that's not possible without testing. If we test our code and find it to be broken (as it often will be), we can then fix it, restoring it to a working state. If we don’t test, we can’t know that our code is broken and there’s a high probability that it just doesn’t work. When we assume that our code works without testing, this can, at best, be an embarrassment. But at worst, it can be a disaster for our company and those that depend on our software (figure 5.1).
Testing gives us confidence that our code works. It gives us peace of mind that our code is safe, is secure, and behaves gracefully under pressure. In this chapter we explore how to automate the testing of our code, indeed how to actually drive our development forward through testing.