We interviewed Dror Helper, who is one of our speakers at ConFoo Vancouver 2016. His presentation is titled ā5 Unit Testing Facts I Wish Iād Known 7 Years Agoā Dror is a senior consultant and evangelist. He mentors developers, speaks at conferences, and teaches clean code, unit testing and TDD. He lives in Israel.
Why is unit testing so important?
I think we got to a point in time in which our industry understands that unit testing is one of the most effective practices a software developer needs to apply when developing modern, working code.
By writing unit tests, a developer is able to really see how his/her code behaves, and if in fact the written code solves the right problem the right way. Writing an extensive test suite creates a safety net against regressions - if a new change or a bug fix breaks my code, I know that a test will fail and so I'm able to develop software more quickly without fearing my code.
What kind of bugs can be prevented by using unit tests?
Since unit tests are a form of "micro requirements" that specify how my code should behave, they tend to catch design and requirement bugs early on. There are also a number of stupid regression bugs that unit tests have saved me from over the years. But it's more than that; unit tests are a design tool. They help me write loosely coupled code and only the code I need to solve the problem at hand.
Should companies invest in unit tests for existing applications?
Only if those applications have clients who need bug fixes and new features. I usually recommend unit testing new development and writing unit (and integration) tests before major refactoring and after each bug fix.
What were your biggest mistakes when starting with unit tests?
Where should I start? I guess that's the reason I've created this session. There are many ways to fail with unit testing: testing the wrong things, not learning about fake objects, thinking that test code and production code have the same design guidelines, and so on.
The important thing is to keep trying and finding new ways to avoid the issues that arise when starting with unit tests.
I think my most important mistake was deciding that "unit tests don't work" after my first failure when I should have asked myself why didn't it work instead.
What is the best advice you can give beginner developers starting with unit testing?
Don't be afraid to try. Just write unit tests, and if they start to break (and not because of a bug), try to understand why. Keep in mind that it's a new discipline you need to master, and as such, you're going to have to find where to acquire new knowledge and find resources you trust.
The good news is that it's not hard as long as you avoid some of the pitfalls, and once you do know how to use unit tests for design, validation, and to avoid regression - you'll never want to go back.
Don't forget to register for your nearest ConFoo conference and follow us on Twitter for more blog posts.