[Tutor] Do you use unit testing?

Kent Johnson kent37 at tds.net
Tue Nov 17 22:32:03 CET 2009

On Mon, Nov 16, 2009 at 3:54 PM, Modulok <modulok at gmail.com> wrote:
> List,
> A general question:
> How many of you guys use unit testing as a development model, or at
> all for that matter?
>  I just starting messing around with it and it seems painfully slow to
> have to write a test for everything you do. Thoughts, experiences,
> pros, cons?

In my opinion TDD is awesome :-) Why?

- It gives you high confidence that your code does what you think it
does, and that you can rely on it
- It can be a lot of fun to write a test, then make it pass,
repeat...each time you make the test pass is a little pat on the back
- good work!
- It forces you to think as a client of the code that you are writing
- It puts pressure on your design, to write code that is
  - testable (duh)
  - loosely coupled
  - reusable (to some extent), because you start with two clients

That's just the benefit when you initially write the code. Just as
important is the benefit downstream, when you want to make a change.
Having a good test suite allows you to make changes to the code (for
example refactoring) with confidence that if you break anything, you
will find out quickly.

This is a profound shift. On projects without tests, there tends to be
code whose function is not clear, or whose structure is tangled, which
no one wants to touch for fear of breaking something. Generally there
is an undercurrent of fear of breakage that promotes code rot.

On projects with tests, fear is replaced with confidence. Messes can
be cleaned up, dead code stripped out, etc. So tests promote a healthy
codebase over time.

There is a cost but it is smaller than it looks from the outside. Yes,
you have to learn to use a test framework, but that is a one-time
cost. I use unittest because it follows the xUnit pattern so it is
familiar across Python, Java, C#, etc. There are alternatives such as
doctest, nose and py.test that some people prefer. I have written a
brief intro to unittest here, with links to comparison articles:

You will occasionally have to stop and figure out how to test
something new and perhaps write some test scaffolding. That time will
pay off hugely as you write tests.

You have to write the tests, but what were you doing before? How do
you know your code works? You must be doing some kind of tests. If you
write your tests as automated unit tests they are preserved and useful
in the future, and probably more comprehensive than what you would do
by hand. If you test by hand, your tests are lost when you finish.

Finally I confess that GUI unit tests are very difficult. I have often
omitted them, instead trying to write a thin GUI over a testable layer
of functionality. In my current (WinForms, C#) project we have found a
workable way to write GUI tests using NUnitForms so in the future
maybe I will be writing more GUI unit tests.

More advocacy:


More information about the Tutor mailing list