[Tutor] unit testing
Tom Plunket
py-tutor@fancy.org
Fri May 30 18:34:02 2003
I have just been getting into Python, but one of the most
infuriating things that I've come across is that I cannot for the
life of me figure out how to easily write unit test code that
automatically gets loaded and imported at runtime. As a C++ pro,
CppUnit did exactly what I wanted it to do, and there was never
any question. PyUnit, however...
My test.py is pretty much ripped off from the regrtest.py found
in Py22's test unit (although I also don't understand why I can't
import test.regrtest):
#!/usr/bin/python, but I'm on Windows. :)
import unittest
import os
if __name__ =3D=3D "__main__":
names =3D os.listdir('.')
tests =3D []
for name in names:
if name[:5] =3D=3D "test_" \
and name[-3:] =3D=3D os.extsep+"py":
modname =3D name[:-3]
tests.append(modname)
tests.sort()
for test in tests:
module =3D __import__(test, globals(), locals(), [])
unittest.main()
Now if I get rid of all of this hooplah and manually=20
import unittest
from test_something import *
from test_somethingelse import *
if __name__ =3D=3D "__main__":
unittest.main()
then everything works. Why isn't my code pulling my tests into
my local namespace? I really don't want to have to maintain this
(with CppUnit I could have test files that had no external
dependencies, I just had to link them into the project and
everything would start getting tests run. I want to just have
all tests in every test_* file run automatically.)
Additionally- why is it that unittest throws an exception when it
ends properly? Why on earth can't it be nice and procedural and
just allow execution to return normally? (Mind you, I am
probably not "getting" Python yet, but that will come!)
thanks.
-tom!