Where did you learn to unit test? How did you learn?

andrew cooke andrew at acooke.org
Tue Apr 29 12:47:51 EDT 2003


as someone who uses java and tests (not sure if they're "unit tests", but
i use junit and test selected classes), i want to second shaleh's point. 
the java type system catches a lot of errors (and generic java, while not
perfect, is a big advance on ordinary java in this respect (generic java
include c++ template like syntax that allows generic containers to be
specialised)).  so there's no (very little) need to test for type
correctness.

that leaves testing for algorithm correctness in the more general sense of
results being what are expected, which is of course useful (and why i do
unit testing in java).

however, when refactoring, it is the type system that i rely on, not the
tests.  the type system is a huge help in refactoring - if i extend an
interface, for example, the compiler will automatically warn me of every
class that (incorrectly) uses the modified interface.  tests are some help
in refactoring, but once the code compiles most of the hard works has been
done.

imho help with refactoring is by far the biggest bonus of static typing
(in a language like java; in more functional languages refactoring tends
to be more about adding higher order functions which, again, are helped by
static typing, but the experience is somewhat different).  this may depend
on your development environment, of course - at the moment i'm working
with a somewhat ugly codebase that needs a lot of refactoring.

returning to the original question - to introduce tests just start using
them.  add them to the main build process in ant (eg by searching for all
classes that end in "Test").  then wait until someone breaks your code. 
at that point they'll trip a test and realise that it's useful.

another example - someone who works for me wrote some code and asked me to
review it.  i wrote some tests and showed him which cases failed.  that
showed him how tests are useful.

(on the flip side, it's a sad fact of life that you sometimes end up
working with crap programmers.  some aren't going to see the advantages of
tests, just as they fail to see the advantage of almost anything else. 
all i can suggest is getting them moved to someone else's project (or
getting moved to someone else's project yourself, depending on whether
they're above or below you on the ladder ;o)).

a key point to making tests work in java is abstracting out test
specifications.  most of the work of test classes is adding frameworks
that allow cases to be specified in a compact manner.  once that is done,
adding test cases is very simple (for example, i verify trees from a
parser against xml specifications - the framework is fairly complex, but
once done it's easy to test many different cases).  this is particularly
important in java because the language itself (the syntax) is fairly
inflexible and verbose.

this approach (lead by example) seems to me much better than forcing
people to use tests.  after all, they're not always appropriate.  and
there's no surer way of making someone dislike somethig than forcing them
to do it.  also, generally, leading by example seems to get you a "better"
team.

andrew

ps xml spec for javacc asts at
http://www.acooke.org/jara/org-acooke/org/acooke/javacc/NodeVerifier.html

Sean 'Shaleh' Perry said:

> On Tuesday 29 April 2003 07:38, Christopher Blunck wrote:
>> Hi all-
>>
>> This is my second generic question posted to c.l.p (the first was 'How
>> many of you are Extreme Programmers?') but it seems like a somewhat
>> appropriate discussion for the group.
>>
>> I've noticed in my development that Python programmers (in general)
>> write more unit tests than their java counterparts.  If I download a
>> project off of jakarta.apache.org, it very rarely has unit tests.
>> However, when I download a python module, it frequently has unit
>> tests.
>>
>
> I believe this is due in part to Python being an interpreted language.
> With
> Java you have a compiler there to catch many problems but with Python most
> bugs, misfeatures, etc. can only be found by exercising the code.  Python
> coding is all about handling the runtime which makes for rapid writing of
> code but almost requires stronger testing.
>
> As Skip said, I got into unit testing by writing test cases to prove bugs
> were
> fixed.  When more bugs were found either new tests were written or old
> ones
> improved.
>
> After that I got into test writing as a way to actually write my
> applications.
> One thing I find hard for me is interface design.  So now I write the code
> that uses my class or function before I write the function.  This helps me
> identify the ways I want the code to work and also provides me with a test
> infrastructure.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
http://www.acooke.org/andrew





More information about the Python-list mailing list