Python中的两个测试工具

开发 后端 测试
本文介绍了两个Python中的测试工具:doctest和unittest,并配以简单的例子来说明这两个测试模块的使用方法,希望能对读者有所帮助。

 [[275103]]

当我们在写程序的时候,我们需要通过测试来验证程序是否出错或者存在问题,但是,编写大量的测试来确保程序的每个细节都没问题会显得很繁琐。在Python中,我们可以借助一些标准模块来帮助我们自动完成测试过程,比如:

  •  unittest: 一个通用的测试框架;
  •  doctest: 一个更简单的模块,是为检查文档而设计的,但也非常适合用来编写单元测试。

下面,笔者将会简单介绍这两个模块在测试中的应用。

doctest

doctest模块会搜索那些看起来像是python交互式会话中的代码片段,然后尝试执行并验证结果。下面我们以doctest.testmod为例,函数doctest.testmod会读取模块中的所有文档字符串,查找看起来像是从交互式解释器中摘取的示例,再检查这些示例是否反映了实际情况。

我们先创建示例代码文件test_string_lower.py,完整代码如下: 

  1. # -*- coding: utf-8 -*-  
  2. def string_lower(string):  
  3.     '''  
  4.     返回一个字符串的小写  
  5.     :param string: type: str  
  6.     :return: the lower of input string  
  7.     >>> string_lower('AbC')  
  8.     'abc'  
  9.     >>> string_lower('ABC')  
  10.     'abc'  
  11.     >>> string_lower('abc')  
  12.     'abc'  
  13.     '''  
  14.     return string.lower()  
  15. if __name__ == '__main__':  
  16.     import doctest, test_string_lower  
  17.     doctest.testmod(test_string_lower) 

首先先对程序进行说明,函数string_lower用于返回输入字符串的小写,函数中的注释中,一共包含了3个测试实例,期望尽可能地包含各种测试情况,接着在主函数中导入doctest, test_string_lower,再运行doctest中的testmod函数即可进行测试。

接着,我们开始测试。首先,在命令行中输入python test_string_lower.py,运行后会发现什么都没有输出,但这其实是件好事,它表明程序中的所有测试都通过了!那么,如果我们想要获得更多的输出呢?可在运行脚本的时候增加参数-v,这时候命令变成python test_string_lower.py -v,输出的结果如下: 

  1. Trying:  
  2.     string_lower('AbC')  
  3. Expecting:  
  4.     'abc'  
  5. ok  
  6. Trying:  
  7.     string_lower('ABC')  
  8. Expecting:  
  9.     'abc'  
  10. ok  
  11. Trying:  
  12.     string_lower('abc')  
  13. Expecting:  
  14.     'abc'  
  15. ok  
  16. 1 items had no tests:  
  17.     test_string_lower  
  18. 1 items passed all tests:  
  19.    3 tests in test_string_lower.string_lower  
  20. 3 tests in 2 items.  
  21. 3 passed and 0 failed.  
  22. Test passed. 

可以看到,程序测试的背后还是发生了很多事。接着,我们尝试着程序出错的情况,比如我们不小心把函数的返回写成了: 

  1. return string.upper() 

这其实是返回输入字符串的大写了,而我们测试的实例却返回了输入字符串的小写,再运行该脚本(加上参数-v),输出的结果如下: 

  1. Failed example:  
  2.     string_lower('abc')  
  3. Expected:  
  4.     'abc'  
  5. Got:  
  6.     'ABC'  
  7. 1 items had no tests:  
  8.     test_string_lower  
  9. **********************************************************************  
  10. 1 items had failures:  
  11.    3 of   3 in test_string_lower.string_lower  
  12. 3 tests in 2 items.  
  13. 0 passed and 3 failed.  
  14. ***Test Failed*** 3 failures. 

这时候,程序测试失败,它不仅捕捉到了bug,还清楚地指出错误出在什么地方。我们不难把这个程序修改过来。

关于doctest模块的更详细的使用说明,可以参考网址:https://docs.python.org/2/library/doctest.html 。

unittest

unittest类似于流行的Java测试框架JUnit,它比doctest更灵活,更强大,能够帮助你以结构化的方式来编写庞大而详尽的测试集。

我们以一个简单的示例入手,首先我们编写my_math.py脚本,代码如下: 

  1. # -*- coding: utf-8 -*-  
  2. def product(x, y):  
  3.     '''  
  4.     :param x: int, float  
  5.     :param y: int, float  
  6.     :return:  x * y  
  7.     '''  
  8.     return x * y 

该函数实现的功能为:输入两个数x, y, 返回这两个数的乘积。接着是test_my_math.py脚本,完整的代码如下: 

  1. import unittest, my_math  
  2. class ProductTestcase(unittest.TestCase):  
  3.     def setUp(self):  
  4.         print('begin test')  
  5.     def test_integers(self):  
  6.         for x in range(-10, 10):  
  7.             for y in range(-10, 10):  
  8.                 p = my_math.product(x, y)  
  9.                 self.assertEqual(p, x*y, 'integer multiplication failed')  
  10.     def test_floats(self):  
  11.         for x in range(-10, 10):  
  12.             for y in range(-10, 10):  
  13.                 xx = x/10  
  14.                 yy = y/10  
  15.                 p = my_math.product(x, y)  
  16.                 self.assertEqual(p, x * y, 'integer multiplication failed')  
  17. if __name__ == '__main__':  
  18.     unittest.main() 

函数unittest.main负责替你运行测试:在测试方法前执行setUp方法,示例化所有的TestCase子类,并运行所有名称以test打头的方法。assertEqual方法检车指定的条件(这里是相等),以判断指定的测试是成功了还是失败了。

接着,我们运行前面的测试,输出的结果如下: 

  1. begin test  
  2. .begin test  
  3.  
  4. ----------------------------------------------------------------------  
  5. Ran 2 tests in 0.001s  
  6. OK 

可以看到,该程序运行了两个测试,每个测试前都会输出'begin test',.表示测试成功,若测试失败,则返回的是F。

接着模拟测试出错的情形,将my_math函数中的product方法改成返回: 

  1. return x + y 

再运行测试脚本,输出的结果如下: 

  1. begin test  
  2. Fbegin test  
  3. F 
  4.  ======================================================================  
  5. FAIL: test_floats (__main__.ProductTestcase)  
  6. ----------------------------------------------------------------------  
  7. Traceback (most recent call last):  
  8.   File "test_my_math.py", line 20, in test_floats  
  9.     self.assertEqual(p, x * y, 'integer multiplication failed')  
  10. AssertionError: -2.0 != 1.0 : integer multiplication failed  
  11. ======================================================================  
  12. FAIL: test_integers (__main__.ProductTestcase)  
  13. ----------------------------------------------------------------------  
  14. Traceback (most recent call last):  
  15.   File "test_my_math.py", line 12, in test_integers  
  16.     self.assertEqual(p, x*y, 'integer multiplication failed')  
  17. AssertionError: -20 != 100 : integer multiplication failed  
  18. ----------------------------------------------------------------------  
  19. Ran 2 tests in 0.001s  
  20. FAILED (failures=2

两条测试都未通过,返回的是F,并帮助你指出了错误的地方,接下来,你应该能快速地修复这个bug。

关于unittest模块的更加详细的说明,可以参考网址:https://docs.python.org/3/library/unittest.html 。

总结

本文介绍了两个Python中的测试工具:doctest和unittest,并配以简单的例子来说明这两个测试模块的使用方法,希望能对读者有所帮助~ 

 

责任编辑:庞桂玉 来源: Python中文社区
相关推荐

2009-09-17 10:11:50

Linux文件Linux系统性能测试

2018-11-19 15:08:21

Python测试工具pytest插件

2019-08-20 08:00:00

JavaScript测试工具前端

2013-11-13 10:49:50

2020-04-17 10:13:51

Python开发工具

2017-09-26 08:51:25

2009-06-26 10:22:58

JSF测试

2011-05-31 18:09:05

动态测试

2018-01-16 11:20:08

2009-03-31 09:49:40

Rational功能测试性能测试

2019-03-15 09:17:22

Web测试工具

2013-08-13 09:43:59

响应式免费测试工具响应式设计

2013-07-26 09:51:12

网站性能网站测试性能测试

2011-10-09 11:00:17

2022-11-28 11:31:37

2012-06-25 14:30:48

Web

2023-10-10 18:20:22

开源API

2024-02-28 08:10:15

Linux静态库动态库

2017-12-22 05:18:06

2019-06-13 05:20:17

点赞
收藏

51CTO技术栈公众号