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

发布于 2024-10-17 03:37:19 字数 1295 浏览 4 评论 0原文

以下 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")

我怎样才能得到它并在子文件夹中搜索测试文件。

也许是这样的:

import sys, os, re, unittest, fnmatch
    for root, dirs, files in os.walk(path):
        for filename in fnmatch.filter(files, pattern):
            print os.path.join(root, filename)

The following python script searches and executes all pyunit tests in the current folder.


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

How can i get it ot search for test files in subfolders.

Maybe something like:

import sys, os, re, unittest, fnmatch

    for root, dirs, files in os.walk(path):
        for filename in fnmatch.filter(files, pattern):
            print os.path.join(root, filename)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-10-24 03:37:19

更改:

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

至:

def regressionTest(somearg):
   path = os.path.abspath(os.path.dirname(somearg))

然后:

for root, dirs, files in os.walk(path):
    regressionTest(root)

Change:

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

To:

def regressionTest(somearg):
   path = os.path.abspath(os.path.dirname(somearg))

And then:

for root, dirs, files in os.walk(path):
    regressionTest(root)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文