一个pyunit例程
import unittest
class customExp(Exception):
def __init__(self,value):
self.value=value
# the target to test
class Widget:
def __init__(self, txt):
self.prop=txt
self.numb = (50,50)
def size(self):
return self.numb
# the testcase
class DefaultWidgetSizeTestCase(unittest.TestCase):
def setUp(self):
print "setUp"
def runTest(self):
print "runTest"
'''this method need to be overrided by testcase
'''
widget = Widget("The widget")
assert widget.size() == (50,50), 'incorrect default size'
print "good"
print __doc__
def tearDown(self):
print "tearDown"
def dododo(self, var):
print "dododod"
print 8088,var
try:
raise ValueError
except ValueError:
raise customExp,"myexception"
else:
print "no no no"
def hello(self):
print "hello"
self.assertEqual(52,52)
self.assertRaises(customExp, self.dododo, "tttttttt")
def testOwn(self):
print "own own own"
# create a testcase
o_testcase1 = DefaultWidgetSizeTestCase()
o_testcase2 = DefaultWidgetSizeTestCase("hello")
# create a testsuite
o_testsuite = unittest.TestSuite()
o_testsuite.addTests([o_testcase1,o_testcase2])
# create a testrunner(it can run testcase or testsuit)
o_runner = unittest.TextTestRunner()
o_runner.run(o_testsuite)
#o_runner.run(o_testcase1)
o_testsuite2=unittest.makeSuite(DefaultWidgetSizeTestCase, "test")
o_runner.run(o_testsuite2)
if __name__ == "__main__":
unittest.main()