Unit testing "templates"?
Raymond Hettinger
vze4rx4y at verizon.net
Sat Apr 12 03:59:58 EDT 2003
"Francis Avila"
> I'm now writing tests for these methods which instantiate the class with
> known data and then call a method of that instance to get a known result.
> But it's getting repetitive, and I'm wondering if there is a better way.>
Whenever you're doing something repetitive, there's *always* a
better way. For a good example, take a look at test_sets.py in Py2.3.
It takes advantage of inheritance to factor out common code. In your
example, it would look like this (untested code ahead):
class DatatypeUnpack(unittest.TestCase):
"""Verify xdmcplib.Unpacker conversions."""
def testUnpack(self):
# unpack should return known result from known valid input
for value, data in self.vdpairs:
obj = xdmcplib.Unpacker(data)
result = getattr(obj, self.method)()
self.assertEqual(value, result)
Class DatatypeUnpackCard32(DatatypeUnpack):
vdpairs = ( (0,'\x00\x00\x00\x00'), (1,'\x00\x00\x00\x01'),
(2,'\x00\x00\x00\x02'),
(65535,'\x00\x00\xff\xff'),
(4294967295L,'\xff\xff\xff\xff') )
method = "unpack_card32"
Class DatatypeUnpackArray8(DatatypeUnpack):
vdpairs = ( ('','\x00\x00'), ('Test','\x00\x04Test') )
method = "unpack_array8"
Raymond Hettinger
More information about the Python-list
mailing list