[Tutor] writing effective unittests

Alan Gauld alan.gauld at btinternet.com
Thu Jan 3 00:35:38 CET 2013


On 02/01/13 20:43, Luke Thomas Mergner wrote:
> I am trying to learn a bit of test-driven programming using unittests and nosetests.

Well done, thats a good idea.

> I am having trouble finding resources that explain how to write effective tests.

Thee are whole books on the subject of testing. If you stick to unit 
test level its not too bad there are some useful resources around.

This is more of a reference sheet than a tutorial but I find it useful...

http://geosoft.no/development/unittesting.html

The main things to remember are that in testing you are trying to break 
your code. Which is a hard thing for any programmer to do because he 
wrote it and loves it and doesn't want to hear that its broken! TDD 
helps get over that because you think about testing before that 
sentimental attachment has fully formed. But the philosophy is the same 
you are trying to think of every which way your  code could break.


Other related topics you could research are
- preconditions
- postconditions
- invariants
- programming by contract

And always remember that just because all your unit tests pass that 
doesn't mean the system as a whole works, it's just more likely to,
and probably easier to fix when it does break.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list