views:

29

answers:

1

If you had to give an overview on unit testing with some practical examples to other colleagues and you yourself weren't up to speed on unit testing, how would you approach that? Examples would be shown in nunit, but I guess it could be any unit testing framework.

+2  A: 

Start writing tests yourself. You are not really in a position to start preaching if you don't do it yourself.

A simple function that is actually harder than it seems might be a good place to start. I remember on a training course an example we implemented was a function to calculate leap years. It is simple but there are quite a few rules.

The Test Driven Development way is to start by writing all the tests you can think of to test the class before writing the class. You can start with a limited set of rules. Make your tests for these basic rules then code up the method. Then add another rule, create more tests before updating the function. When you change the function you will see how the old tests are being used to test that adding the new rule hasn't broken the old rules.

Another good thing to show is how testable code is actually better code. It is hard testing classes that have lots of dependencies but having lots of dependencies is generally bad practice.

Adam Butler