I write tests to inspire confidence. I think like this 100% of the time. Why do I need confidence? If I write a section of code that is responsible for abstracting the access to the data layer for the rest of the application, I write tests to determine that I’m putting and retrieve valid data coupled with proper handling of errors. These tests generally aren’t written in a test first manner as they grow as my knowledge of the domain grows. If I’m implementing an algorithm from a paper or off the Internet, I will generally write them in a TDD fashion. And finally, if I’m going back and reviewing code coverage, I’ll write tests or refactor to ensure I have the proper coverage.
At first, I was writing to only ensure that I was proving my code was correct in a particular section. Now I write them because I know I can’t trust myself unless they are present. This is why my tests inspire confidence.