regression.py源代码分析

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

"""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.

This program is part of "Dive Into Python", a free Python book for

experienced programmers. Visit http://diveintopython.org/ for the

latest version.

"""

__author__ = "Mark Pilgrim ([email protected])"

__version__ = "$Revision: 1.4 $"

__date__ = "$Date: 2004/05/05 21:57:19 $"

__copyright__ = "Copyright (c) 2001 Mark Pilgrim"

__license__ = "Python"

import sys, os, re, unittest

%这里同时导入四个模块:sys(为系统函数和得到命令行参数),os(为目录列表之类的操作系统函

%数),re(为正则表达式),以及unittest(为单元测试)。

def regressionTest():

path = os.path.abspath(os.path.dirname(sys.argv[0]))

%将第一个命令行参数作为目录名,返回该目录名所在的绝对地址,然后赋给path

files = os.listdir(path)

%列举path目录下的所有文件,然后赋值给files变量

test = re.compile("test\.py$", re.IGNORECAS)

%匹配以“test.py”结尾的文件

files = filter(test.search, files)

%获得所有以“test.py”结尾的文件

filenameToModuleName = lambda f: os.path.splitext(f)[0]

%lambda的作用:用lambda表达式写:

# f(x, y) = x + y
f = lambda x, y : x + y

%此处的作用即:给定参数f,获得f的文件名,注意不要扩展名,若想获得扩展名,则应使用

% os.path.splittext(f)[1]

moduleNames = map(filenameToModuleName, files)

%map函数的作用:它接受函数和列表作为参数,然后返回函数处理之后的列表

modules = map(__import__, moduleNames)

%利用moduleNames所包含的模块名来导入模块,你可以通过map__import__的协同工作,将模块名 (字符串) 映射到实际的模块 (像其他模块一样可以被调用和使用)

load = unittest.defaultTestLoader.loadTestsFromModule

%内省每一个模块并为每个模块返回一个unittest.TestSuite对象。每个TestSuite(测试套件) 对象都包 含一个TestCase对象的列表,每个对象对应着你的模块中的一个测试方法

return unittest.TestSuite(map(load, modules))

%将TestSuite列表封装成一个更大的测试套件

if __name__ == "__main__":

unittest.main(defaultTest="regressionTest")

你可能感兴趣的:(python,正则表达式,OS,F#,单元测试)