Thursday, April 6, 2017

Tests: beyond verification

I must admit that my first contact with software testing was not so long ago. Since them I've been increasingly impressed with how powerful a good suite of tests can be for verifying if we are delivering the expected product. Yet, as Uncle Bob well asserts, verification is just one of the benefits of writing tests. This is especially true when we're talking about TDD. There are at least four other (arguably more important) reasons that motivate writing tests first whenever possible:

  • Avoid regressions. A well constructed suite of tests gives much more freedom when we need to extend or refactor existing code. It gives us a safe guard against changes that can inadvertently break the working software.
  • Documentation. I've never thought about this before, but tests are a form of living and executable documentation. They have to stay current, otherwise they'll break. Besides, they show how to work with the code, instead of just inform how to do it. They're not only a form of documentation, but a very good one.
  • Better design. Writing tests forces us to think by the point of view of the caller of our code. Not only that, it forces us to write client code for our own code. This can have beneficial impacts in our architectural decisions because many issues related to the software interface become more evident as we write the tests (and ahead of time if we write them first, which is even better).
  • Better implementation. This topic is closely related to the previous one, but deserves its own section in face of the great improvement that it can represent for a software project. Citing Uncle Bob again: "the act of writing tests first forces us to decouple the software!". Tightly coupled software is bad. It's hard to maintain, hard to extend and often hard to understand. It's also very hard to test. This way, in order to test it we have to decouple it. The more testable it is, the more decoupled it is. And the more decoupled it is, the better designed and implemented it is.

For more details about these topics I strongly recommend reading chapter 4 of Agile Software Development: Principles, Patterns, and Practices. Words of wisdom from Robert Martin.

No comments

Post a Comment