Reflections on TDD

August 1, 2016 Testing

I’ve been doing test driven development for a while on a GitLab CI. I have to say I even liked that! I think, every developer should try to write tests before code, and here’s why.

Reasons

More testable code

(Not) surprisingly, writing tests before the production code requires you to actually think how to make it easy to test. For example, would you intentionally use static methods in a unit test? I don’t think so. That would require one to use PowerMock or something similar… Not trivial at all.

Early errors discovery

Unit tests are a great way of checking whether a function started (or stopped) working as expected. Frequently modules are tightly coupled, and changing one module changes the behaviour of another, totally unrelated at first glance, function. Tests allow you to catch such errors before they go to production.

Better test coverage

Would anyone test all edge cases of an already working code? Probably implementing some new functionality would be considered more productive. Meanwhile it’s so easy to miss a small, stupid bug in any code base, that would require you to spend hours with a debugger.

Further resources

There is a book about TDD by Kent Beck: Test Driven Development: By Example. I recommend reading it in case you are interested in learning more.

comments powered by Disqus