通过上面的分析,文章二,三,四,即创建testcase,testsuit,开始运行run,这几个步骤,我们可以自己来指定,很简单。
class MyTest(unittest.TestCase):
def setUp(self):
print("Set up...")
self.myclassa = MyClassA.MyClassA()
def testsum(self, a = 4, b = 5):
self.assertEqual(self.myclassa.addMy(a, b), a * b)
def testmul(self, a = 4, b = 5):
self.assertEqual(self.myclassa.mulMy(a, b), a * b)
def tearDown(self):
print("Tear down...")
pass
mytestcase = MyTest()
mytestcase.myclassa = MyClassA.MyClassA()
mytestcase.testsum(5,6)
mytestcase.testmul(5,6)
def __init__(self, methodName='runTest'):
"""Create an instance of the class that will use the named test
method when executed. Raises a ValueError if the instance does
not have a method with the specified name.
"""
self._testMethodName = methodName
self._outcome = None
self._testMethodDoc = 'No test'
try:
testMethod = getattr(self, methodName)
except AttributeError:
if methodName != 'runTest':
# we allow instantiation with no explicit method name
# but not an *incorrect* or missing method name
raise ValueError("no such test method in %s: %s" %
(self.__class__, methodName))
else:
self._testMethodDoc = testMethod.__doc__
self._cleanups = []
self._subtest = None
testcase1 = MyTest("testsum")
testcase2 = MyTest("testmul")
runner = unittest.TextTestRunner()
runner.run(testcase1)
runner.run(testcase2)