• unittest单元测试流程


    整理了一个单元测试的完整流程,觉得很不错。

    一个 test case
    类应该派生自 unittest.TestCase正常的的test case的调用顺序为

     

    from __future__ import print_function
    import unittest
     
    def setUpModule():
        print("setUpModule")
      
    def tearDownModule():
        print("tearDownModule")
         
    class MyTestCase(unittest.TestCase):
     
        def setUp(self):
            super(MyTestCase, self).setUp()
            self.addCleanup(self.my_cleanup)
             
            print()
            print("++++ setup")
     
        def tearDown(self):
            super(MyTestCase, self).tearDown()
            print("++++ tearDown")
            raise Exception()
             
        def my_cleanup(self):
            print("++++ my_cleanup")
             
        def test_case_1(self):
            print("++++ test case 1")
     
        def test_case_2(self):
            print("++++ test case 2")
               
        @classmethod
        def setUpClass(cls):
            print("setUpClass")
     
        @classmethod
        def tearDownClass(cls):
            print()
            print("tearDownClass")

    运行结果
    setUpModule
    setUpClass
     
    ++++ setup
    ++++ test case 1
    ++++ tearDown
    ++++ my_cleanup
     
    ++++ setup
    ++++ test case 2
    ++++ tearDown
    ++++ my_cleanup
     
    tearDownClass
    tearDownModule

    来自 <http://www.2cto.com/kf/201409/330721.html>

  • 相关阅读:
    09 python初学 (字符串)
    08 python 初学(字典)
    07 Python初学(元组)
    ubuntu 学习
    10 python 初学(Python 的编码解码)
    12 python 初学(深浅拷贝、集合)
    11 python初学 (文件)
    ubuntu 在 Windows 下的安装
    mysql常用命令总结
    关于Windows 7 下临时IPV6地址的问题,如何禁用它
  • 原文地址:https://www.cnblogs.com/semishigure/p/7283411.html
Copyright © 2020-2023  润新知