Python Unitest 源码阅读:概述

最近学习python 的单元测试,好奇之下阅读了一下unittest模块的源码。稍作记录作为梳理,帮助自己加深理解和回忆


一,用法

unittest模块的用法很简单,定义个 TestCase的子类,然后根据需要重载Setup()和TearDown()两个方法。并且在类中定义一些测试用例条目的“方法” ,以test开头命名即可
例如,下面这一段代码(来自:http://blog.chinaunix.net/uid-22334392-id-3887031.html,也是我第一次用unittest参考的博文)

import kk
import unittest
class ProductTestCase(unittest.TestCase):
    def setUp(self):
        print 'units test begin'+'#'*20
    def tearDown(self):
        print 'unittest end'+'#'*20
    def testxx(self):
        print 'this is just test'
    def testIntegerProduct(self):
        for x in xrange(-10,10):
            for y in xrange(-10,10):
                p = kk.product(x,y)
                self.failUnless(p == x*y,'integer multiplication failture')
    def testFractalProduct(self):
        for x in xrange(-10,10):
            for y in xrange(-10,10):
                x = x/10.0
                y = y/10.0
                p = kk.product(x,y)
                self.failUnless(p == x*y,'fractal multiplication failture')

if __name__ == '__main__':
    unittest.main()
其中kk为被测模块
注意到代码中的testIntegerProduct(self) 及 testFractalProduct(self) 两个以 test开头命名的方法即是测试用例条目,整个类为 TestCase的子类,unittest通过这两个信息(红色标注部分)来识别测试用例和用例下的条目。
通过调用unitest.main()方法来启动单元测试。


二.unittest 模块流程概述

首先明确unittest中对测试用例的封装,即TestSuit  是 TestCase类的一个“集合”:
源码中作者对TestSuit的注释:
class TestSuite(object):
    """A test suite is a composite test consisting of a number of TestCases.

    For use, create an instance of TestSuite, then add test case instances.
    When all tests have been added, the suite can be passed to a test
    runner, such as TextTestRunner. It will run the individual test cases
    in the order in which they were added, aggregating the results. When
    subclassing, do not forget to call the base class constructor.
    """
往TestSuit内增加TestCase的代码片段为:
    def addTest(self, test):
        # sanity checks
        if not hasattr(test, '__call__'):
            raise TypeError("the test to add must be callable")
        if isinstance(test, type) and issubclass(test, (TestCase, TestSuite)):
            raise TypeError("TestCases and TestSuites must be instantiated "
                            "before passing them to addTest()")
        self._tests.append(test)

    def addTests(self, tests):
        if isinstance(tests, str):
            raise TypeError("tests must be an iterable of tests, not a string")
        for test in tests:
            self.addTest(test)

其中形参test 是TestCase的实例。
明确这一点后,unittest的大体流程其实是比较好理解的:
1.调用unittest.main() 
    调用 unittest中的main方法其实是创建TestProgram类的实例
main = TestProgram


##############################################################################
# Executing this module from the command line
##############################################################################

if __name__ == "__main__":
    main(module=None)
TestProgram类“实施”了unittest的全部流程(创建测试用例,执行测试用例,输出结果):

2.解析参数和构造TestSuit
unittest支持命令行参数,TestProgram通过调用parseArgs()方法完成参数的解析
并且根据传入参数来对TestSuit进行构建,传递的参数可以包含到具体的测试用例或测试条目 例如支持这样的命令:python Myunittest.py Mytestcase.testsomething 等
所以paresArgs()方法会根据不同的参数调用不同的加载测试用例的方法:有loadTestFromMoudle() 、loadTestFromName()、loadTestFromCase()等,这些方法都属于TestLoader类。
需要提前知道的是,这些Load测试用例的方法返回的都是TestSuit类。
  def parseArgs(self, argv):
        import getopt
        long_opts = ['help','verbose','quiet']
        try:
            options, args = getopt.getopt(argv[1:], 'hHvq', long_opts)
            for opt, value in options:
                if opt in ('-h','-H','--help'):
                    self.usageExit()
                if opt in ('-q','--quiet'):
                    self.verbosity = 0
                if opt in ('-v','--verbose'):
                    self.verbosity = 2
            if len(args) == 0 and self.defaultTest is None:
                self.test = self.testLoader.loadTestsFromModule(self.module)# 通过调用loadTestFromModule加载测试用例
                return
            if len(args) > 0:
                self.testNames = args
            else:
                self.testNames = (self.defaultTest,)
            self.createTests()#若指定测试用例参数,则执行createTest(),即调用loadTestFromName
        except getopt.error as msg:
            self.usageExit(msg)

    def createTests(self):
        self.test = self.testLoader.loadTestsFromNames(self.testNames,
                                                       self.module)

3.加载完测试用例即获得TestSuit后,则执行runtest方法开始执行测试
 def runTests(self):
        if isinstance(self.testRunner, type):
            try:
                testRunner = self.testRunner(verbosity=self.verbosity)
            except TypeError:
                # didn't accept the verbosity argument
                testRunner = self.testRunner()
        else:
            # it is assumed to be a TestRunner instance
            testRunner = self.testRunner
        self.result = testRunner.run(self.test)
        if self.exit:
            sys.exit(not self.result.wasSuccessful())


随后将分为“加载测试用例” “执行测试用例”记录对unittest的理解



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值