第 16 章 有效编程(Functional Programming)

16.1. 概览

第 13 章 单元测试 中,你学会了单元测试的哲学。在 第 14 章 以测试优先为原则的编程 中你步入了 Python 基本的单元测试操作,在 第 15 章 重构 部分,你看到单元测试如何另大规模重组变得容易。 本章将在这些程序范例的基础上,集中关注于超越单元测试本身的更高级的 Python 特有技术。

下面是一个作为简单退化(regression)测试框架运行的完整 Python 程序。 它将你前面编写的单独单元测试模块组织在一起成为一个测试套件并一次性运行。实际上这是我构建本书自身代码的一部分。我为几个样例程序都编写了单元测试。(不是只有 第 13 章 单元测试 中的 roman.py 模块),我的自动构建代码的第一个工作便是确保我所有的例子可以正常工作。 如果退化测试程序失败,构建过程当即终止。 我可不想因为发布了不能工作的样例程序而让你在下载他们后坐在显示器前抓耳挠腮地为程序不能运转而烦恼。

例 16.1. regression.py

如果您还没有下载本书附带的例子程序, 可以 下载本程序和其他例子程序

"""Regression testing framework

This module will search for scripts in the same directory named
XYZtest.py.  Each such script should be a test suite that tests a
module through PyUnit.  (As of Python 2.1, PyUnit is included in
the standard library as "unittest".)  This script will aggregate all
found test suites into one big test suite and run them all at once.
"""

import sys, os, re, unittest

def regressionTest():
    path = os.path.abspath(os.path.dirname(sys.argv[0]))   
    files = os.listdir(path)                               
    test = re.compile("test\.py$", re.IGNORECASE)          
    files = filter(test.search, files)                     
    filenameToModuleName = lambda f: os.path.splitext(f)[0]
    moduleNames = map(filenameToModuleName, files)         
    modules = map(__import__, moduleNames)                 
    load = unittest.defaultTestLoader.loadTestsFromModule  
    return unittest.TestSuite(map(load, modules))          

if __name__ == "__main__":                   
    unittest.main(defaultTest="regressionTest")

把这段代码放在本书其他样例代码相同的目录下运行之,模块 test.py 中的所有单元测试将被找到并一起被运行。

例 16.2. regression.py 的范例输出

[you@localhost py]$ python regression.py -v
help should fail with no object ... ok                             1
help should return known result for apihelper ... ok
help should honor collapse argument ... ok
help should honor spacing argument ... ok
buildConnectionString should fail with list input ... ok           2
buildConnectionString should fail with string input ... ok
buildConnectionString should fail with tuple input ... ok
buildConnectionString handles empty dictionary ... ok
buildConnectionString returns known result with known input ... ok
fromRoman should only accept uppercase input ... ok                3
toRoman should always return uppercase ... ok
fromRoman should fail with blank string ... ok
fromRoman should fail with malformed antecedents ... ok
fromRoman should fail with repeated pairs of numerals ... ok
fromRoman should fail with too many repeated numerals ... ok
fromRoman should give known result with known input ... ok
toRoman should give known result with known input ... ok
fromRoman(toRoman(n))==n for all n ... ok
toRoman should fail with non-integer input ... ok
toRoman should fail with negative input ... ok
toRoman should fail with large input ... ok
toRoman should fail with 0 input ... ok
kgp a ref test ... ok
kgp b ref test ... ok
kgp c ref test ... ok
kgp d ref test ... ok
kgp e ref test ... ok
kgp f ref test ... ok
kgp g ref test ... ok

----------------------------------------------------------------------
Ran 29 tests in 2.799s

OK
1 前五个测试来自于 apihelpertest.py,用以测试 第 4 章 自省的威力 中的范例代码。
2 接下来的五个代码来自于 odbchelpertest.py, 用以测试 第 2 章 第一个 Python 程序 中的范例代码。
3 其他的代码来自于 romantest.py,你在 第 13 章 单元测试 中深入学习过。