如何使附加的pyunit python脚本在子文件夹中搜索测试

如何使附加的pyunit python脚本在子文件夹中搜索测试,python,python-unittest,Python,Python Unittest,下面的python脚本搜索并执行当前文件夹中的所有pyunit测试 """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

下面的python脚本搜索并执行当前文件夹中的所有pyunit测试

"""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) print 'You are HERE! ' ,os.getcwd() 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") 回归测试框架 此模块将在名为的同一目录中搜索脚本 每个这样的脚本都应该是一个测试套件,用于测试 模块到PyUnit。(从Python 2.1开始,PyUnit包含在 标准库为“unittest”。)此脚本将聚合所有 在一个大的测试套件中找到测试套件并同时运行它们。 """ 导入系统、操作系统、re、单元测试 def regressionTest(): path=os.path.abspath(os.path.dirname(sys.argv[0])) files=os.listdir(路径) 打印“你在这里!”,os.getcwd() test=re.compile(“.test\.py$”,re.IGNORECASE) 文件=过滤器(test.search,文件) filenameToModuleName=lambda f:os.path.splitext(f)[0] moduleNames=map(文件名到模块名,文件) 模块=映射(导入,模块名称) load=unittest.defaultTestLoader.loadTestsFromModule 返回unittest.TestSuite(映射(加载、模块)) 如果名称=“\uuuuu main\uuuuuuuu”: unittest.main(defaultTest=“回归测试”) 如何在子文件夹中搜索测试文件来获取它

可能是这样的:

import sys, os, re, unittest, fnmatch 导入系统、操作系统、re、单元测试、fnmatch 对于os.walk(路径)中的根、目录和文件: 对于fnmatch.filter中的文件名(文件、模式): 打印os.path.join(根目录,文件名) 更改:

def regressionTest():
   path = os.path.abspath(os.path.dirname(sys.argv[0]))
致:

然后:

for root, dirs, files in os.walk(path):
    regressionTest(root)
你可以用它。它几乎自动地为您完成这一切。
def regressionTest(somearg):
   path = os.path.abspath(os.path.dirname(somearg))
for root, dirs, files in os.walk(path):
    regressionTest(root)