16.1先测试 后编码


16.1.1 精确的需求说明


16.1.2 为改变而计划

覆盖度是测试知识中重要的部分。,优秀的测试程序组的目标之一是拥有良好的覆盖度,实现这个目标的方法之一是使用覆盖度工具


16.2测试工具

其中有两个很棒的模块可以协助你自动完成测试过程:

1.unitest:通用测试框架

2.doctest:简单一些的模块,是检查文档用的,但是对于编写单元测试也很在行。



16.2.1 doctest

例如 假设求数字平方的函数,并且在文档字符串中添加了一个例子

def square(x):

Squares a number and returns the result.

>>>square(2)

4

>>>square(3)

9

...

return x*x


文档字符串中也包括了一些文本。这和测试又有什么关系?假设square函数定义在my_math模块中。


if __name__=='__main__':

import doctest,my_math

doctest.testmod(my_math)

可以只导入doctest和my_math模块本身,然后运行doctest中的testmod函数。

$python my_math_.py



16.2.2 unitest

使用unittest框架的简单测试

import unittest,my_math

class ProductTestCase(unittest.TestCase):

def testIntegers(self):

for x in xrange(-10,10):

for y in xrange(-10,10):

  p = my_math.product(x,y)

  self.failUnless(p == x*y,'Integer multiplication failed')

def testFloats(self):

for x in xrange(-10,10):

for y in xrange(-10,10):

x = x/10.0

y = y/10.0

p = my_math.product(x,y)

self.failUnless(p == x*y,'Float multiplication failed')

if __name__ == '__main__':unittest.main()


unittest.main函数负责运行测试。它会实例化所有TestCase子类,运行所有名字以test开头的方法。

如果定义了叫做setUP和tearDown的方法,它们就会在运行每个测试方法之前和之后执行,这样就可以用这些方法为所有测试提供一般的初始化和清理代码,这被称为测试夹具。

unittest模块会区分由异常引发的错误和调用failUnless等函数而导致的失败。下一步就是编写概要代码,这样一来就没错误---只有失败。这就意味着要创建一个包含下列内容的模块my_math。

def product(x,y):

pass

下一步让测试代码工作

def product(x,y):

return x * y

接下来修改product函数,让它针对特定的数值7和9失败。

def product(x,y):

if x==7 and y==9:

return 'An insidious bug has surfaced!'

else:

return x * y


16.3.1 使用PyChecker和PyLint检查源代码

pychecker file.py


pylint module


PyChecker和PyLint都可以作为模块导入,但是两者并不是真正为程序设计的。当导入pychecker.checker时,它会检查之后的代码,并且在标准输出中打印警告。pylint.lint模块有个叫做Run的非文档记录型函数,可以在pylint脚本本身中使用。


使用subprocess模块调用外部检查模块

import unittest,my_math

from subprocess import Popen,PIPE

class ProductTestCase(unittest.TestCase):

def testWithPyCheck(self):

cmd = 'pychecker','-Q',my_math.__file__.rstrip('c')

pychecker = Popen(cmd,stdout=PIPE,stderr=PIPE)

self.assertEqual(pychecker.stdout.read(),'')

def testWithPyLint(self):

cmd = 'pylint','-rm','my_math'

pylint = Popen(cmd,stdout=PIPE,stderr=PIPE)

self.assertEqual(pylint.stdout.read(),'')

if __name__ == '__main__':unittest.main()

上面已经给出了检查程序的几个命令行开关,以避免无关的输出干扰测试:对于pychecker来说,提供了-Q选项,而对于pylint,我提供了-rn(n意为no)关闭报告,也就是说只显示警告和错误。这里使用了assertEqual函数以便使从stdout特性读取的真正输出显示在unittest的失败信息中。

pylint命令会直接同给定名称的模块一起运行,所以简单多了。为了能让pycheck工作正常,我们还能获取一个文件名。使用my_math模块的__file__属性获取这个值,用rstrip剔除任何文件名末尾中可能出现的字符c。

为了能让PyLint不出现错误,我忽略重写了my_math模块。

"""

A simple math module

"""

__revision__ = '0.1'

def product(factor1,factor2):

'The product of two numbers'

return factor1 * factor2


16.3.2 分析

标准库中已经包含了一个叫做profile的分析模块。使用分析程序非常简单,是要使用字符串参数调用它的run方法就行了

>>>import profile

>>>from my_math import product

>>>profile.run('product(1,2)')

如果提供了文件名,比如'my_math.profile'作为第二个参数来运行,那么结果就会保存到文件中。可以在之后使用pstats模块检查分析结果:

>>>import pstats

>>>p = pstats.Stats('my_math.profile')