
1、setUp准备环境。
执行每个测试用例的前提条件。
2、tearDown恢复环境。
执行每个测试用例的后置条件。
3、setUpClass所有case执行的前置条件,只运行一次。
必须使用@classmethod装饰器,
4、tearDownClass所有case运行后只运行一次。
必须使用@classmethod装饰器,
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import unittest
#要继承unittest.TestCase
class CalcTestcase(unittest.TestCase):
def setUp(self) -> None:
print( "我是setUp" )
def test01(self):
print( "我是的测试方法test01" )
def test02(self):
print( "我是的测试方法test02" )
def test03(self):
print( "我是的测试方法test03" )
def tearDown(self) -> None:
print( "我是tearDown" )
@classmethod
def setUpClass(cls) -> None:
print( "我是setUpClass" )
@classmethod
def tearDownClass(cls) -> None:
print( "我是tearDownClass" )
if __name__ == '__main__' :
unittest.main()
#设置套件
# suite = unittest.TestSuite()
# #把测试方法添加到集合中,然后循环取值,在添加到套件里面输出
# list = ["test01","test02","test03"]
# for i in list:
# suite.addTest(CalcTestcase(i))
|
以上就是python单元测试中的函数整理,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。