Monday, October 12, 2009

Automated Testing and Why You Should Do It

I first learned about automated unit testing (in the style of extreme programming) about four years ago, and decided to experiment with it. I quickly found out that code I thought I had thoroughly debugged had errors in it. Since then, I've become quite fond of automated testing. While it's not perfect, it does help prevent some of the worst errors, such as the one that I saw happen today.

Every time I've set up automated tested of my code, I've found and fixed mistakes that would be embarrassing to have other people find. Today, the same happened to a colleague of mine.

He spent the past 6 months working on an internal project. The diff was 6000 lines long. He touched 74 files for this project. In other words, this was a very large project, time consuming, arduous, and draining. Today was the big day to roll it out into production.

Suffice to say that things did not go well. A single debugging line slipped through the cracks. That debugging line wound up providing normal users with access that they should not have. A single line inside of a 6000 line diff caused a huge issue at work for him.

Had the environment been using automated testing, at least one of those tests would have been checking for unauthorized access.  Running those tests would have revealed the bug well before it went to production systems.

Instead, a huge ruckus ensued. He now feels embarrassed. He knows I'm writing this post, and feels like he deserves the humiliation. I happen to disagree with him on that point. He worked within the constraints of his environment, and with the tools he was given.

I'm hoping that that environment will change some procedures in the near future, and start to use automated unit testing. It may well slow you down somewhat during the development phase, but you will feel more confident in your results, and your code will have fewer bugs.

For your own sake, learn how to use the tools from your language of choice. Find out how to do automated testing, and start doing it. You will only benefit from knowing your code works as designed. Some links that can help you get started:

Test Driven Development, Perl, Python, Java C++, Selenium

Also, some empirical research into the benefits: Test Driven Development reduces defects by 40% to 90%. Finally, a link to several papers about the benefits of test driven development.

Go, read those links, and learn how test driven development can save you from significant embarrassment.

1 comment:

Chris Arndt said...

Jonathan Hartley did a great introductory talk on TDD with Python: http://tartley.com/?p=312