Python程序自测 | 阿小信的博客
阿小信大人的头像
Talk is cheap. Show me the code. Linus Torvalds

Python程序自测2014-09-02 05:18

doctest

doctest其实就是在docstring中按照解释器的格式写出正确运行的效果,实际测试时如果与预计不同就会报错。如下

:::python
#mymodule.py
def split(line, types=None, delimiter=None):
    """
    >>> split('ashin chen xiao baidu')
    ['ashin', 'chen', 'xiao', 'baidu']
    >>> split('ashin 256 512.0', [str, int, float])
    ['ashin', 256, 512.0]
    >>> split('ashin,chen,xiao,baidu', delimiter=',')
    ['ashin', 'chen', 'xiao', 'baidu']
    >>> 
    """

    fields = line.split(delimiter)
    if types:
        fields = [ty(val) for ty, val in zip(types, fields)]
    return fields

def half(x):
    """
    >>> half(6.8)  #改为整数就不会有任何输出
    3.4
    >>> 
    """
    return x/2

if __name__ == '__main__':
    import doctest
    doctest.testmod()

输出:

**********************************************************************
File "mymodule.py", line 19, in __main__.half
Failed example:
    half(6.8)
Expected:
    3.4
Got:
    3.3999999999999999
**********************************************************************
1 items had failures:
   1 of   1 in __main__.half
***Test Failed*** 1 failures.

外部测试

#test.py
import mymodule
import doctest

nfail, ntests = doctest.testmod(mymodule)
print nfail, ntests #0 3

unittest

unittest是用断言判断实际结果是否等于预测结果。测试方法必须以test....开头,后面的随意。

#-*- coding:utf-8 -*-

import mymodule
import unittest

class TestSplitFunc(unittest.TestCase):
    def setUp(self):
        #执行设置操作
        pass
    def tearDown(self):
        #执行清除操作
        pass
    def testsimplestring(self):
        r = mymodule.split('hello world')
        self.assertEqual(r, ['hello', 'world'])
    def testtypeconvert(self):
        r = mymodule.split('ashin 100 200.0', [str, int, float])
        self.assertEqual(r, ['ashin', 100, 200.0])
    def testdelimiter(self):
        r = mymodule.split('a,b,c', delimiter=',')
        self.assertEqual(r, ['a', 'b', 'c'])

#运行unittest
if __name__ == '__main__':
    unittest.main()

正确输出:

...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

不给限定参数报错输出如下:

F.F
======================================================================
FAIL: testdelimiter (__main__.TestSplitFunc)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "t.py", line 21, in testdelimiter
    self.assertEqual(r, ['a', 'b', 'c'])
AssertionError: ['a,b,c'] != ['a', 'b', 'c']

======================================================================
FAIL: testtypeconvert (__main__.TestSplitFunc)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "t.py", line 18, in testtypeconvert
    self.assertEqual(r, ['ashin', 100, 200.0])
AssertionError: ['ashin', '100', '200.0'] != ['ashin', 100, 200.0]

----------------------------------------------------------------------
Ran 3 tests in 0.001s

FAILED (failures=2)
shell returned 1

另一种执行doctest的方法是使用unittest创建测试程序

import doctest
import unittest
import ttt

suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(ttt))
#or 
#suite = unittest.TestLoader().loadTestsFromTestCase(ttt.ccc) 
runner = unittest.TextTestRunner()
print(runner.run(suite))

如果您觉得从我的分享中得到了帮助,并且希望我的博客持续发展下去,请点击支付宝捐赠,谢谢!

若非特别声明,文章均为阿小信的个人笔记,转载请注明出处。文章如有侵权内容,请联系我,我会及时删除。

#Python#  
分享到:
阅读[1148] 评论[0]

你可能也感兴趣的文章推荐

本文最近访客

发表评论