Justin Francis Self-Portrait

Saturday, July 14, 2007

Unit Test Pep Talk

The last two major features we developed at work were built largely without writing unit tests, let alone with a test-driven mentality. In fact, I would occasionally explicitly mention that unit tests should be written for such and such an issue, and it would turn out that the tests were not written. This led, predictably, to a much buggier release than normal, with patches being pushed to production every day for over a week.

Talking this over with my colleague, I felt that this was because while the whole team aknowledges that unit tests are theoretically a good idea, they feel that sometimes it is better to move faster and write less tests. So we decided to run down the reasons why our team unit tests using test-driven development in our iteration de-briefing. Here is what we said. Some of this will certainly not be novel, but bear in mind that these are the practical benefits that we see everyday that cause us to require unit tests in the software we write.

First, the obvious. Writing unit tests reduces bugs and speeds development by actually running your code quickly and checking it against the expected result automatically. I actually saw members of our team doing this manually: running the software and seeing if the code busted.

Probably the most important thing for me is that writing unit tests allow you to refactor safely. Without a solid suite of unit tests, it is impossible to go in to a module with a cleaver and be sure that you are not breaking anything.

Finally, and this was certainly not obvious to our team, the unit tests form a contract for what the code should be doing. If I am wondering what a method's behavior should be (as opposed to what it is), I go and look at the unit tests. They will tell me what is required of that method, and what is not required. I use this all the time for critical system configuration issues. For example, if a value must be 2 on system startup for the sytem to operate correctly, I add a unit test so that if ever anyone decides to change that value, the unit tests will tell them that is not allowed.

Moving on to test-driven development, we mentioned two reasons. The first and most important is that practicing test-driven development ensures adequate test-case coverage. By requiring a failing test before writing code, you ensure that every branch of your code is tested for a desired response. Similar to my first point about unit testing in general, this is simply the automation of something we do anyway: come up with the expected outcome of our code and then write the solution until we arrive at the expected outcome.

More subtly, test-driven development will improve the design of your code. Code that is tightly coupled, or not cohesive is much harder to test than the alternative. By writing tests first, you envision how you would like to use the library, instead of forcing yourself to use whatever interface you came up with after the fact. Because you want to isolate the code you are testing, and minimize the amount of test code you write, test-drive developement encourages a modular, re-usable design.

I feel that stressing the practical, immediate benefits of test-driven development is the best way to convince those who may believe, but do not know that automated testing makes their lives easier. It is so easy to write poor unit tests, and so hard to monitor, it is clear this is preferable to forcing the practice, even if it is official policy.


Anonymous said...

Sorry for my bad english. Thank you so much for your good post. Your post helped me in my college assignment, If you can provide me more details please email me.

chenmeinv0 said...

michael kors outlet clearance
burberry outlet
louis vuitton purses
rolex watches outlet
louboutin pas cher
mbt shoes
louboutin shoes
coach outlet online
canada goose sale
cheap uggs
michael kors outlet
montblanc pens
cheap ugg boots
louis vuitton outlet
chanel outlet
fitflop sandals
coach outlet online
michael kors
coach outlet
adidas trainers uk
ugg outlet
jordan shoes
cavaliers jerseys
tiffany and co jewelry
cheap rolex watches
doudoune canada goose
chaussures ugg
ray ban sunglasses outlet
cheap jordan shoes
louis vuitton handbags

Obat Penambah Berat Badan said...

It is great to have visited your website. Thanks for sharing useful information. And also visit my website about health. God willing it will be useful too

Cara Menghilangkan Bopeng Bekas Jerawat
Cara Membersihkan flek di Paru-paru
Pengobatan Herbal Rematik/Rheumatiod Arthritis
Obat Herbal Walatra Berry Jus
Cara Mengobati Telapak Kaki Berlubang
Obat Koreng Bernanah Alami

samaher said...

شركة نقل عفش من الرياض الى جدة

شركة نقل عفش من الرياض الى الامارات شركة شحن عفش من الرياض الى الامارات
شركة نقل عفش من الرياض الى الاردن شركة شحن عفش من الرياض الى الاردن

شركة نقل عفش من الرياض الى الدمام

8jxs0vq3nc said...

So, in the end run|ultimately}, it’s all about your luck in the particular person on-line playing roulette session. The ‘en prison’ rule is one other chance to win back the cash positioned on a shedding even-money guess when the ball lands on zero. These rules cut back the house edge to 1.35% however usually are not all the time included in all French Roulette variations. Caesars NJ on-line casino provides traditional styles of on-line roulette, from American to French, nicely as|in addition to} some particular roulette video games like Double Bonus Spin Roulette. To guarantee your security, you must to} play only at on-line roulette sites with a UKGC licence. Whether you’re completely new to the game or have been playing in} for some time, our staff of roulette experts 1xbet are here to show you the basics of the game and allow you to get higher.