[Tutor] unit testing

Sean 'Shaleh' Perry shalehperry@home.com
Fri, 14 Sep 2001 10:49:37 -0700 (PDT)


On 14-Sep-2001 Timothy Wilson wrote:
> Hi everyone,
> 
> 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?
> 

Unit testing at its simplest is ensuring that each function acts sanely.  So
you end up writing things like:

value = is_leap('1999')
if value != false:
        print "is_leap failure on parameter 1999"

i.e. pass it a value you know the answer for.  Now this is a silly case you
would really like to make sure they got things like every 400 years or not when
the moon is blue.  In other words pick places where the algorithm could have
been broken.  This is not easy for beginners and whether they use pyunit or not
it will still be a task for them to learn.  What my profs told us was pass
values to test every if, else and while.

In a language like python what I typically do is write my functions then start
the interpreter and load my code as a module and run the functions dynamically.
 This has spoiled me to no end and makes writing C painful now.