• 技术文章 >Python技术 >Python基础教程

    python单元测试中的函数整理

    小妮浅浅小妮浅浅2021-11-03 10:15:47原创23055

    1、setUp准备环境。

    执行每个测试用例的前提条件。

    2、tearDown恢复环境。

    执行每个测试用例的后置条件。

    3、setUpClass所有case执行的前置条件,只运行一次。

    必须使用@classmethod装饰器,

    4、tearDownClass所有case运行后只运行一次。

    必须使用@classmethod装饰器,

    实例

    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电脑。

    专题推荐:python 单元测试
    品易云
    上一篇:python中condition条件变量的作用 下一篇:python中lambdas匿名函数的用法

    相关文章推荐

    • python ndarray数组对象有什么特点• python函数中使用for循环• python变量赋值的注意点• python执行数据库的查询操作• python元类冲突的问题• python os.system执行cmd指令• python os.popen方法是什么• python中subprocess的用法• 如何走进Python的大门?• python蒙特卡洛算法的介绍• python如何过滤列表中的唯一值• python列表推导式的结构探究• python中condition条件变量的作用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网