前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 单元测试 & 文档测试

Python 单元测试 & 文档测试

作者头像
py3study
发布2020-01-15 18:11:51
1K0
发布2020-01-15 18:11:51
举报
文章被收录于专栏:python3python3

1.1 单元测试

1.1.1 单元测试编写

单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。

编写一个Dict类,这个类的行为和dict一致,但是通过属性来访问。

>>> d = Dict(a=1, b=2)

>>> d['a']

1

>>> d.a

1

class dict编写如下:

[root@daidai python]# cat mydict.py

#!/usr/bin/python

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

class Dict(dict):

   def __init__(self, **kw):

       super().__init__(**kw)

   def __getattr__(self, key):

       try:

           return self[key]

       except KeyError:

           raise AttributeError(r"'Dict' has no attribute %s." % key)

   def __setattr__(self, key, value):

       self[key] = value

编写单元测试:

[root@daidai python]# cat mydict_test.py

#!/usr/bin/python

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

import unittest

from mydict import Dict

class TestDict(unittest.TestCase):     #测试类从unittest.TestCase继承

   def test_init(self):            #不是以test开头的方法不被执行

       d = Dict(a = 1, b = 'test')

       self.assertEqual(d.a, 1)

       self.assertEqual(d.b, 'test')

       self.assertTrue(isinstance(d, dict))

   def test_key(self):

       d = Dict()

       d['key'] = 'value'

       self.assertEqual(d.key, 'value')

   def test_attr(self):

       d = Dict()

       d.key = 'value'

       self.assertTrue('key' in d)

       self.assertEqual(d['key'], 'value')

   def test_keyerror(self):

       d = Dict()

       with self.assertRaises(KeyError):  #通过d['empty']访问不存在的key时,断言会抛出KeyError

           value = d['empty']

   def test_attrerror(self):

       d = Dict()

        with self.assertRaises(AttributeError): #通过d.empty访问不存在的key时,我们期待抛出AttributeError

           value = d.empty

if __name__ == '__main__':

unittest.main()     #运行单元测试

1.1.2 运行单元测试

[root@daidai python]# python mydict_test.py #加上unittest.main()运行语句

.....

----------------------------------------------------------------------

Ran 5 tests in 0.001s

OK

[root@daidai python]#

[root@daidai python]#

[root@daidai python]# python -m unittest mydict_test  #不用加上unittest.main()运行语句

.....

----------------------------------------------------------------------

Ran 5 tests in 0.001s

OK

1.1.3 setUp() & tearDown()

这两个方法分别在单元测试中每个测试方法的前后被执行。

[root@daidai python]# cat mydict_test.py

#!/usr/bin/python

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

import unittest

from mydict import Dict

class TestDict(unittest.TestCase):

def setUp(self):

        print('test begins...')

   def test_init(self):

       d = Dict(a = 1, b = 'test')

       self.assertEqual(d.a, 1)

       self.assertEqual(d.b, 'test')

       self.assertTrue(isinstance(d, dict))

   def test_key(self):

       d = Dict()

       d['key'] = 'value'

       self.assertEqual(d.key, 'value')

   def test_attr(self):

       d = Dict()

       d.key = 'value'

       self.assertTrue('key' in d)

       self.assertEqual(d['key'], 'value')

   def test_keyerror(self):

       d = Dict()

       with self.assertRaises(KeyError):

           value = d['empty']

   def test_attrerror(self):

       d = Dict()

       with self.assertRaises(AttributeError):

           value = d.empty

def tearDown(self):

        print('test ends...')

#if __name__ == '__main__':

#   unittest.main()

[root@daidai python]# pythonmydict_test.py    #发现这样测试没有作用

[root@daidai python]# python -m unittestmydict_test

test begins...

test ends...

.test begins...

test ends...

.test begins...

test ends...

.test begins...

test ends...

.test begins...

test ends...

.

----------------------------------------------------------------------

Ran 5 tests in 0.001s

OK

1.2 文档测试

当我们编写注释时,如果写上这样的注释:

def abs(n):

   '''

   Function to get absolute value of number.

   Example:

   >>> abs(1)

    1

   >>> abs(-1)

    1

   >>> abs(0)

    0

   '''

   return n if n >= 0 else (-n)

无疑更明确地告诉函数的调用者该函数的期望输入和输出。

并且,Python内置的“文档测试”(doctest)模块可以直接提取注释中的代码并执行测试。

doctest严格按照Python交互式命令行的输入和输出来判断测试结果是否正确。只有测试异常的时候,可以用...表示中间一大段烦人的输出。

让我们用doctest来测试上次编写的Dict类:

# mydict2.py

class Dict(dict):

   '''

   Simple dict but also support access as x.y style.

   >>> d1 = Dict()

   >>> d1['x'] = 100

   >>> d1.x

   100

   >>> d1.y = 200

   >>> d1['y']

   200

   >>> d2 = Dict(a=1, b=2, c='3')

   >>> d2.c

   '3'

   >>> d2['empty']

   Traceback (most recent call last):

       ...

   KeyError: 'empty'

   >>> d2.empty

   Traceback (most recent call last):

       ...

   AttributeError: 'Dict' object has no attribute 'empty'

   '''

   def __init__(self, **kw):

       super(Dict, self).__init__(**kw)

   def __getattr__(self, key):

       try:

           return self[key]

       except KeyError:

           raise AttributeError(r"'Dict' object has no attribute '%s'" %key)

   def __setattr__(self, key, value):

       self[key] = value

if __name__=='__main__':

   import doctest

   doctest.testmod()

运行python3 mydict2.py:

$ python3 mydict2.py

什么输出也没有。这说明我们编写的doctest运行都是正确的。如果程序有问题,比如把__getattr__()方法注释掉,再运行就会报错:

$ python3 mydict2.py

**********************************************************************

File "/Users/michael/Github/learn-python3/samples/debug/mydict2.py",line 10, in __main__.Dict

Failed example:

   d1.x

Exception raised:

   Traceback (most recent call last):

     ...

   AttributeError: 'Dict' object has no attribute 'x'

**********************************************************************

File"/Users/michael/Github/learn-python3/samples/debug/mydict2.py", line16, in __main__.Dict

Failed example:

   d2.c

Exception raised:

   Traceback (most recent call last):

     ...

   AttributeError: 'Dict' object has no attribute 'c'

**********************************************************************

1 items had failures:

   2of   9 in __main__.Dict

***Test Failed*** 2 failures.

注意到最后3行代码。当模块正常导入时,doctest不会被执行。只有在命令行直接运行时,才执行doctest。所以,不必担心doctest会在非测试环境下执行。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.1 单元测试
    • 1.1.1 单元测试编写
      • 1.1.2 运行单元测试
        • 1.1.3 setUp() & tearDown()
        • 1.2 文档测试
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档