您当前位置:首页 > 深入 Python > 函数式编程 | << >> | ||||
深入 Python从 Python 新手到专家 |
在第 13 章“单元测试”中,您学习了单元测试的理念。在第 14 章“测试优先编程”中,您逐步完成了Python 中基本单元测试的实现。在第 15 章“重构”中,您看到了单元测试如何使大规模重构更容易。本章将以这些示例程序为基础,但我们将更多地关注高级Python 特定技术,而不是单元测试本身。
以下是一个完整的Python 程序,它充当了一个廉价且简单的回归测试框架。它获取您为各个模块编写的单元测试,将它们全部收集到一个大型测试套件中,并一次性运行它们。我实际上将此脚本用作本书构建过程的一部分;我为几个示例程序编写了单元测试(不仅仅是第 13 章“单元测试”中介绍的 roman.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")
在与本书附带的其他示例脚本相同的目录中运行此脚本,将找到所有名为 moduletest.py 的单元测试,将它们作为单个测试运行,并一次性通过或失败所有测试。
[you@localhost py]$ python regression.py -v help should fail with no object ... okhelp 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
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
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
![]() |
前 5 个测试来自 apihelpertest.py,它测试第 4 章“自省的力量”中的示例脚本。 |
![]() |
接下来的 5 个测试来自 odbchelpertest.py,它测试第 2 章“您的第一个 Python 程序”中的示例脚本。 |
![]() |
其余的来自 romantest.py,您在第 13 章“单元测试”中对此进行了深入研究。 |
<< 总结 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
寻找路径 >> |