Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取当前包中所有模块的列表_Python_Unit Testing_Module_Packages_Python 2.5 - Fatal编程技术网

Python 获取当前包中所有模块的列表

Python 获取当前包中所有模块的列表,python,unit-testing,module,packages,python-2.5,Python,Unit Testing,Module,Packages,Python 2.5,下面是我想做的:我想构建一个测试套件,它被组织成tests.ui、tests.text、tests.fileio等包。在每个\uuuuinit\uu.py包中,我想制作一个测试套件,包括该包中所有模块中的所有测试。当然,可以使用unittest.TestLoader完成所有测试,但似乎我必须单独添加每个模块。因此,假设test.ui有editor\uucode>window\ucode>test.py和preview\ucode>window\ucode>test.py,我希望\ucode>in

下面是我想做的:我想构建一个测试套件,它被组织成tests.ui、tests.text、tests.fileio等包。在每个
\uuuu
init
\uu
.py包中,我想制作一个测试套件,包括该包中所有模块中的所有测试。当然,可以使用unittest.TestLoader完成所有测试,但似乎我必须单独添加每个模块。因此,假设test.ui有editor
\uucode>window
\ucode>test.py和preview
\ucode>window
\ucode>test.py,我希望
\ucode>init
\ucode>.py导入这两个文件并获得两个模块对象的列表。我的想法是,我想自动生成测试套件,这样我就不会忘记在测试套件中包含一些东西

最好的方法是什么?这似乎是件容易的事,但我什么也没找到


顺便说一句,我正在使用Python 2.5。

您可以使用os.listdir查找test.*目录中的所有文件,然后过滤掉.py文件:

# Place this code to your __init__.py in test.* directory
import os
modules = []
for name in os.listdir(os.path.dirname(os.path.abspath(__file__))):
    m, ext = os.path.splitext()
    if ext == '.py':
        modules.append(__import__(m))
__all__ = modules
魔法变量
\uuuu file\uuuu
包含当前模块的文件路径。试一试

print __file__

要检查。

您可以使用os.listdir查找test.*目录中的所有文件,然后过滤掉.py文件:

# Place this code to your __init__.py in test.* directory
import os
modules = []
for name in os.listdir(os.path.dirname(os.path.abspath(__file__))):
    m, ext = os.path.splitext()
    if ext == '.py':
        modules.append(__import__(m))
__all__ = modules
魔法变量
\uuuu file\uuuu
包含当前模块的文件路径。试一试

print __file__

要检查。

我们django项目中此问题的解决方案:

"""Test loader for all module tests
"""
import unittest
import re, os, imp, sys

def find_modules(package):
    files = [re.sub('\.py$', '', f) for f in os.listdir(os.path.dirname(package.__file__))
             if f.endswith(".py")]
    return [imp.load_module(file, *imp.find_module(file, package.__path__)) for file in files]

def suite(package=None):
    """Assemble test suite for Django default test loader"""
    if not package: package = myapp.tests # Default argument required for Django test runner
    return unittest.TestSuite([unittest.TestLoader().loadTestsFromModule(m)
                               for m in find_modules(package)])

if __name__ == '__main__':
    unittest.TextTestRunner().run(suite(myapp.tests))

编辑:与bialix的解决方案相比,它的优点是您可以将此加载程序放在项目树中的任意位置,无需在每个测试目录中修改init.py。

解决django项目中的此问题:

"""Test loader for all module tests
"""
import unittest
import re, os, imp, sys

def find_modules(package):
    files = [re.sub('\.py$', '', f) for f in os.listdir(os.path.dirname(package.__file__))
             if f.endswith(".py")]
    return [imp.load_module(file, *imp.find_module(file, package.__path__)) for file in files]

def suite(package=None):
    """Assemble test suite for Django default test loader"""
    if not package: package = myapp.tests # Default argument required for Django test runner
    return unittest.TestSuite([unittest.TestLoader().loadTestsFromModule(m)
                               for m in find_modules(package)])

if __name__ == '__main__':
    unittest.TextTestRunner().run(suite(myapp.tests))

编辑:与bialix的解决方案相比,它的好处是您可以将此加载程序放在项目树中的任意位置,无需在每个测试目录中修改init.py。

这里的答案很好,但最好的方法是使用第三方测试发现和运行程序,如:

  • (我最喜欢的)
  • (非常好,尤其是在测试异步内容时)
  • (我认为不太好)
它们都与plain unittest.TestCase兼容,您不必以任何方式修改测试,也不必使用其中的高级功能。仅用作套件发现


您想在这些LIB中重新发明讨厌的东西,有什么具体原因吗?

这里的答案很好,但最好是使用第三方测试发现和运行程序,如:

  • (我最喜欢的)
  • (非常好,尤其是在测试异步内容时)
  • (我认为不太好)
它们都与plain unittest.TestCase兼容,您不必以任何方式修改测试,也不必使用其中的高级功能。仅用作套件发现


你想在这些libs中重新发明讨厌的东西,有什么特别的原因吗?

(像OP想要的那样自动导入子模块有点不和谐。将
导入a,b,c
放在init.py中也很容易)。总之,缺少步骤:
modules.append(\uuuu import\uuuuuu(name))
,然后
\uuuuu all\uuuuu=modules
。这似乎也是通过filepath导入的,而filepath只在2.6之前的python中起作用。这不是bug,而是相对导入。(像OP想要的那样自动导入子模块有点不太方便。将
导入a、b、c
放在init.py中也很容易)。总之,缺少步骤:
模块。追加(\uuuuu导入\uuuu(名称))
,然后是
\uuuuuuuuuuuuuuuuuuuuuuu=modules
。这似乎也是通过filepath导入的,这只是因为在2.6之前的python中出现了一个bug。这不是bug,而是相对导入。不,不是真的。我不知道它们。:-,不,不是真的。我不知道他们的情况。:-)近似重复