Why and When Do I Test?

This is my motivation to test software and when I do it.

When Do I write tests?

Writing tests means writing automated tests: more code to run and test your code.

I write tests when I think it'll help more than hurt. Tests are not free. There are tradeoffs. Writing tests doesn't always make sense.

"Test Always" Approach

I think a "test always" approach is wasteful. Because the tests aren't free. You're likely to waste time and effort and money because you over-test or over-complicate. This is likely over-indulgence in testing, a failure to think.

"Test Never" Approach

I think the "tests are a waste" approach is also naive. You can waste time and effort and money by not having enough tests or not testing the needful things or testing in a low-value way. This is likely an allergy to testing as a concept, a failure to think.

Why Do I Write Tests?

The main reasons I end up writing tests are to

  • Verify the system

  • Save me time

  • Help me write code

Verify the System

I usually want to know that I wrote the program correctly the first time.

I want to know that I didn't break the program when things change.

I want to be more sure about the integrity of the system when the stakes are higher. In these cases, I'll often write tests.

I can know this without automated tests. And sometimes having automated tests doesn't mean I know this.

Save Me Time

I'll write tests if it'll help me think faster in design.

I'll write tests if it'll save me time in the maintenance cycle as things change and get refactored.

I'll write tests if if it'll let me sleep instead of getting paged.

Help Me Write Code

Sometimes writing tests helps me write the program itself. The test will help me think through the problem, often step by step, and not miss cases. I often like using TDD, test-driven development, in these cases.

Are Test Helpful?

Yes, they can be. They can help me verify the system, save time and help me write code.

Using the best tool for the job can mean writing some automated tests.

Play the odds. Consider the tradeoffs. No magic bullets here. Let the tool help you. Don't be an ideological slave to testing or not testing.