Where it really shines is in an environment with a library or set of libraries that interact with each other to give the programs actions.
The current place I work use a complicated system written in python. I have been asked to make some changes inside this. Naturally my first thought was to isolate the particular functions I have to change in a unit test
The change I have to make isn't important to know for this article but I may as well explain it briefly. The modules implement both client and server for a system transmitting data over a variety of transports. Currently the bulky part of the data is sent with UDP. For a number of reasons too tedious to recount this needs to be rewritten to use TCP/IP instead. As UDP is in many ways a simpler and more basic protocol the changes will be moderately complicated
After a bit of effort I managed to get a test program working that more or less compiles using the python "unittest" module. "unittest" works in the familar way that perl .t files work or Kent Becks xunit stuff or junit or...I could go on. The doctest method seems more popular in the python community but I prefer doing stand alone unittest programs
So I did a unittest program which like I said, compiled. But it didn't run. It was throwing an error somewhere in the guts of the modules. If I was trying to debug the production code directly ( I was doing this last week :) ), for example with an error at line 36 of the foo module then I'd run this
python -m pdb service
(Pdb) b foo.py:36
Breakpoint 1 at /home/jamie/fix/foo.py:36
(Pdb) c
The program would run and then break at where the error was so variables can be inspected etc. In some situations this is a very useful method
However, if I try this on a program that is a unittest it simply gives me a cryptic message about "The program exited via sys.exit(). Exit status: False", it doesn't stop at the requested line and gives no clues.
I tried to use the "debug" method mentioned in the unittest documentation. I tried all kinds of things in the (Pdb) shell but with no helpful effect.
I did STFW for a long time before I found a comment from "orip" at Stackoverflow
This suggests using the testoob python module. So I installed it and now, running
testoob --debug datachanneltest.pyRuns my unittest program AND opens a (Pdb) line when an error occurs, allowing variable inspection etc. Fantastic!
Of course a fix that marvellous and easy has it's problems. If there is an error in a library function like the socket getpeername() it can't quite manage to DTRT but seems to jam up inside the call and doesn't allow access to the python stack above. Maybe if i knew more Pdb voodoo it might.
Summary: testoob is recommended for running tests with the python unittest module