前言
此系列笔记涉及python 3.6 unittest的学习。包括内容:后面补充吧
测试准备
一个简单类,提供 两个数加法和乘法的两个方法:MyClassA.py
class MyClassA(object):
def __init__(self):
#print("Initate {}".format(self.__class__.__name__))
pass
def addMy(self, a, b):
#print("Current func: {}".format(sys._getframe().f_code.co_name))
return a + b
def mulMy(self, a, b):
#print("Current func: {}".format(sys._getframe().f_code.co_name))
return a * b
我们故意写个测试失败的用例,在testsum(...)中,后面写成a*b(a+b就pass了)
import unittest
from pkg_jd import MyClassA
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
if __name__ =='__main__':
unittest.main()
直接运行这个utamy.py: 输出见下面:
Set up...
.Tear down...
Set up...
Tear down...
F
======================================================================
FAIL: testsum (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\hy\ws\Prj_jd\pkg_jd\utamy.py", line 13, in testsum
self.assertEqual(self.myclassa.addMy(a, b), a * b)
AssertionError: 9 != 20
----------------------------------------------------------------------
Ran 2 tests in 0.031s
FAILED (failures=1)