zoukankan      html  css  js  c++  java
  • 三言两语聊Python模块–单元测试模块unittest

    实际上unittest模块才是真正意义上的用于测试的模块,功能强大的单元测试模块。

    继续使用前面的例子:

    # splitter.py
    def split(line, types=None, delimiter=None):
        """Splits a line of test and optionally performs type conversion.
        For example:
    
        >>> split('GOOD 100 490.50')
        ['GOOD', '100', '490.50']
        >>> split('GOOD 100 490.50', [str, int, float])
        ['GOOD', 100, 490.50]
        >>>
        By default, splitting is perfomed on whitespace, but a different delimiter
        can be selected with the delimiter keyword argument:
    
        >>> split('GOOD, 100, 490.50', delimiter=',')
        ['GOOOD', '100', '490.50']
        >>>
        """
    
        fields = line.split(delimiter)
        if types:
            fields = [ty(val) for ty, val in zip(types, fields)]
        return fields
    
    if __name__ == '__main__':
        # test myself
        import doctest
        doctest.testmod()
    

     编写测试该函数的脚本:

    # testsplitter.py
    import splitter
    import unittest
    
    # unit test
    class TestSplitFunction(unittest.TestCase):
        def setUp(self):
            pass
        def tearDown(self):
            pass
        def testsimplestring(self):
            r = splitter.split('GOOD 100 490.50')
            self.assertEqual(r, ['GOOD', '100', '490.50'])
        def testypeconvert(self):
            r = splitter.split('GOOD 100 490.50', [str, int, float])
            self.assertEqual(r, ['GOOD', 100, 490.50])
        def testdelimiter(self):
            r = splitter.split('GOOD, 100, 490.50', delimiter=',')
            self.assertEqual(r, ['GOOD', '100', '490.50'])
    
    # Run unit test
    if __name__ == '__main__':
        unittest.main()
    

     运行结果:

    >>> 
    F..
    ======================================================================
    FAIL: testdelimiter (__main__.TestSplitFunction)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "C:UsersAdministratorDesktopPython Scripts	estsplitter - 副本.py", line 19, in testdelimiter
        self.assertEqual(r, ['GOOD', '100', '490.50'])
    AssertionError: Lists differ: ['GOOD', ' 100', ' 490.50'] != ['GOOD', '100', '490.50']
    
    First differing element 1:
     100
    100
    
    - ['GOOD', ' 100', ' 490.50']
    ?           -       -
    
    + ['GOOD', '100', '490.50']
    
    ----------------------------------------------------------------------
    Ran 3 tests in 0.059s
    
    FAILED (failures=1)
    Traceback (most recent call last):
      File "C:UsersAdministratorDesktopPython Scripts	estsplitter - 副本.py", line 23, in <module>
        unittest.main()
      File "D:Python33libunittestmain.py", line 125, in __init__
        self.runTests()
      File "D:Python33libunittestmain.py", line 267, in runTests
        sys.exit(not self.result.wasSuccessful())
    SystemExit: True
    

     unittest里面unitest.TestCase实例t最常用的方法:

    t.setUp() - 执行测试前的设置步骤

    t.tearDown() - 测试完,执行清除操作

    t.assertEqual(x, y [,msg]) - 执行断言

    还有其他各种断言。

  • 相关阅读:
    电脑设置开机
    python 环境搭建 python-3.4.4
    遍历hashmap 的四种方法
    Java8 使用 stream().map()提取List对象的某一列值及排重
    解决 SpringMVC 非spring管理的工具类使用@Autowired注解注入DAO为null的问题
    CXF之"@XmlType.name 和 @XmlType.namespace 为类分配不同的名称"错误
    java.lang.NoSuchMethodError: javax.wsdl.xml.WSDLReader.readWSDL
    java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern
    java.lang.IllegalArgumentException: Request header is too large
    ie8 报错:意外地调用了方法或属性访问
  • 原文地址:https://www.cnblogs.com/herbert/p/3244580.html
Copyright © 2011-2022 走看看