How to organize test cases with PyUnit
Donnal Walter
donnal at donnal.net
Sat Jul 20 08:41:15 EDT 2002
Peter Hansen <peter at engcorp.com> wrote:
> Roy Smith wrote:
> > Peter Hansen <peter at engcorp.com> wrote:
> > > Rename the runTest()
> > > method to start with the text "test", and then change the __main__
> > > content to be simply 'unittest.main()' instead of the dictEntries.run
> > > that you have.
> >
> > OK, that seemed to work, even though I'm still not sure I understand all
> > the subtleties of what's going on, and what role all the various classes
> > (TestCase, TestSuite, TestResult, TestLoader, etc) play.
>
> I don't really either, but I've never needed to. Basically just subclass
> TestCase and do the above and you may never need to either. :)
Yes, it's all very confusing to me. I love unit tests, but I've been
unable to grok the API for TestSuites. Let's say I have a module named
"cell.py" and a corresponding test module "unit.cell_unit.py" with the
following code:
import unittest
from cell import Cell
class Test(unittest.TestCase):
def test1(self):
x = Cell()
self.failUnless(isinstance(x, Cell))
def test2(self):
assert_(1 != 2)
if __name__ == '__main__':
unittest.main()
This is just as Peter suggested above, and I can run this unit test
from the command line just fine. But, now let's say that I want to
make it part of a test suite by adding the following code to cell.py:
if __name__ == '__main__':
import unittest
import unit.cell_unit
ts = unittest.TestSuite()
ts.addTest(unit.cell_unit.Test())
unittest.TextTestRunner().run(ts)
Now I get the following error:
ValueError: no such test method in unit.cell_unit.Test: runTest
unless I add a runTest definition to cell_unit.Test:
def runTest(self):
self.test1()
self.test2()
.
.
.
Somehow I don't think this is how it was intended to be used, but so
far it is the only way I can figure out to make it work. Can anyone
tell me what am I doing wrong?
More information about the Python-list
mailing list