在上次不成功的重构以后,我又蠢蠢欲动的重新设计程序,纠结了许久,还是决定程序写成OO的,OO设计我还没怎么想好,不过这次的重新设计我只写业务逻辑的API,同时编写配套的单元测试,用单元测试来验证API的正确
程序结构
common/bussiness/data/test/ testMain.pytestMain.py是整个测试的启动点,只要在test目录执行该文件,那么testMain.py就会查找test目录下其他文件,并加载文件里的测试用例一并执行
#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport sysimport unittestlistFiles = []def getTestFiles(pathRoot): listPath = os.listdir(pathRoot) for pathName in listPath: p = pathName.find(".") if p == -1: path = pathRoot+"/"+pathName sys.path.append(path) getTestFiles(path) else: filename = pathName[:p] if filename == 'testMain': continue listFiles.append(filename)getTestFiles(".")listFiles = list(set(listFiles))#print listFiles#loadTestCasesfor file in listFiles: command = "from %s import *" % file #print command exec(command)if __name__ == "__main__": unittest.main()