Factored out some test code

This is not based on your latest but it's a simple demonstration of the ideas.
Between this and the docs for the unittest module I think you can get going quickly with testing.
To see what it does run 'python test.py'.
(not all tests pass).