views:

34

answers:

2

In my shop, there isn't much familiarity with unit testing in general, but I'd like to get into it, at the very least, as a proof of concept. Would it be a crazy idea to simply use the Visual Studio function Create Unit Tests on my small (~500 lines) code base and show the concept that way?

+3  A: 

Not a crazy idea at all, just know what you should expect to get from that approach.

If you just want to show how to unit test existing code, which is what Roy Osherove calls TAD (Test After Development), it is a quick and easy way to show what kind of things should be tested.

If you are unit testing because you are interested in moving towards TDD, then your approach may serve to confuse, rather than enlighten. Trying to show someone TDD by adding unit tests to existing code defeats the purpose, in my opinion.

Rather, I would look into something (like SpecFlow) that attempts to focus on the spirit of TDD, which is that you're writing a specification (or requirement) in your test code, before you write the real code to make it work.

A light switch flipped for me when I realized that a unit test written before code could actually be my very detailed requirement. It was self-documenting, and as long as the unit test (spec) was written correctly, then my working code was guaranteed to meet the requirement!

If you agree with this, you may want to take a look at the Bowling Game Kata. I found it very helpful for introducing unit testing and TDD concepts.

Noah Heldman
A: 

No this is not crazy at all and is IMHO a great idea.

The only way a group or company can get into a culture of Unit Testing is if someone starts using it. If you don't it's unlikely that the existing people who aren't familiar with the concept will.

A potentially even better idea would be to grab a couple bugs on your plate and write the unit tests which reproduce the failure. Demo the tests catching the real bug and validating the solution. I find that's often fairly compelling for people who aren't familiar with the concept of unit testing.

JaredPar