When is unit-testing bad? [was: Re: does lack of type...]
edvard+web at majakari.net
Mon Jun 23 12:40:00 CEST 2003
I'm rather interested in (and fond of) unit testing as well as TDD, so I
though to share my opinions. I once discussed TDD with my friend in here,
and the common agreement seemed to be that TDD is not a silver bullet -
often there are types of computational modules that are very hard to unit
test, eg. think of graphical user interfaces or complex systems, where
forking or threading is concerned.
I tried to think of quick rule when to write unit tests for a module or
not, and I only came up with this: if the whole module works like a
set of functions (may be only one f, of course) where
1. Input is easily representable as native language data structures or
combination of those
2. Output is easily representable as native language data structures or
combination of those
3. Relationship between those two is deterministic
Then I thought that if number of all (obvious) test cases is very large,
it may not be worth it to write unit tests for all of them if the
functions/methods concerned are simple enough.
For example, a software that reads XML and converts certain some XML
snippets to Python data structures may contain very large number of
combinations considering all possible valid XML files, but it may be still
worth the trouble to write unit tests for at least most typical
situations. You can always add more tests later.
I think one of the best things in writing unit tests is how it helps to
avoid doing same mistakes twice. When you encounter a bug, you add
appropriate test case to your test suite and fix it until all tests pass
again. By running unit tests before every release you can be sure that all
known bugs have been corrected (assuming every accepted bug results in new
written test case).
Some people seem to have lukewarm attitude towards unit testing and
TDD. No, TDD is not a silver bullet and neither unit testing nor TDD - or
both combined - can guarantee a 100% bug-free software product, but it
sure helps a lot and is a nice method in addition to many others in an
attempt to design, implement and maintain high-quality software products.
Just my 2 cents,
# Edvard Majakari Software Engineer
# PGP PUBLIC KEY available Soli Deo Gloria!
One day, when he was naughty, Mr Bunnsy looked over the hedge into Farmer
Fred's field and it was full of fresh green lettuces. Mr Bunnsy, however, was
not full of lettuces. This did not seem fair. --Mr Bunnsy has an adventure
More information about the Python-list