Testing programs is tedious - but very necessary! Testing lets us trust our code - and unit-tests (via 'nosetests') make the task of writing tests super-easy. We should all be writing unit-tests with our code.
This is a paid-for series, buy access to all 1 hour 47 minutes of the series via lulu.com. Cost: £5 GBP (approximately $10 US, 8 Euro). 10% of the purchase price will be reserved as a donation to the Python Software Foundation (link). We offer a 100% refund within 30 days of purchase if you are unhappy with the series (no questions asked).
Here we use the excellent 'nosetests' to adopt a test-driven approach to coding, allowing us to build our confidence that our code is working exactly as we want it to. If you don't have nosetests yet, see Installing Nosetests in the wiki.
We build 'checkNumbers.py' and a corresponding unit-test module 'testCheckNumbers.py' (source code in the wiki) that confirms that our program - written to parse a file of numbers and check that the numbers match our specified criteria - passes all the tests and only accepts the kind of numbers that we are after.
We use boolean conditions and exception handling to robustly parse the file of numbers, along with commenting our code and using sensible variable names. One of the nice features of TDD is writing tests before we have the corresponding features in our program, watching the tests fail, then filling in the necessary code in the program to pass the tests (hence test *driven* development). This is a very satisfying approach to coding.
I end the video giving a perspective on just how many unit-tests you should be writing to sensibly test your code. Additionally you should watch Jeff Winkler's ShowMeDo on the nosey automatic test runner after you've watched this video (don't forget to leave him a Thank-you comment!).
At the end of this set you will have the necessary knowledge to write your own unit-tests, allowing you to be confident that your code runs (and fails) in the right way - this is a key software development practice.
If you want more details see this worked-example at DiveIntoPython and this nose write-up. For some more background, see wikipedia for entries on Unit Testing and Test Driven Development (TDD).
Having reached the end of this series, come join me in the forum for questions, tasks and to let me know what you'd like me to cover in a future set.