[Tutor] unit testing

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 14 Sep 2001 16:04:00 -0700 (PDT)


On Fri, 14 Sep 2001, Timothy Wilson wrote:

> I'd like to introduce the concept of unit testing to my beginning
> progamming students as early in the year as possible. I think they'll
> be motivated to learn it because our first project was to write a
> little leap year calculator. They spent a lot of time entering the
> same years over and over again while testing their program logic.
>
> I don't think they're quite ready for PyUnit. Does anyone have any
> useful resources or hints about doing basic unit testing?

One "obvious" tip: if your students are testing a boolean function, then
they should show at least that the function has the potential of returning
a false value.


As a somewhat contrived example, here's a somewhat broken function:

###
def isConsonant(letter):
    return letter != 'aeiou'
###

A hasty student could point out that this works pretty well:

###
>>> isConsonant('f')
1
###

but it pays to have a student show how to get isConsonant() to return 0 as
well.  Make sure to tell trigger-happy students to slow down and to make
sure their tests make some sort of sense... *grin*

Unit testing is good because it emphasises the fact that programming can
be something experimental --- most of us are human, and we often need to
test our own inventions to increase our confidence.  But unit testing
depends on the quality of the test.

I think it's important to stress that the correctness of a program isn't
dependent just in its "positive" results, but also in its "negative" ones.

Hope this helps!