获取当前软件包中所有模块的列表 [英] Getting a list of all modules in the current package

查看:84
本文介绍了获取当前软件包中所有模块的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:我想构建一个测试套件,该套件被组织成包,例如tests.ui,tests.text,tests.fileio等.在这些包的每个__ init __ .py中,我想创建一个测试套件,其中包含该软件包中所有模块中的所有测试.当然,可以使用unittest.TestLoader完成所有测试,但是看来我必须单独添加每个模块.因此,假设test.ui具有编辑器_ window _ test.py和Preview _ window _ test.py,我希望__ init __ .py导入这两个文件并获取两个模块对象的列表.我的想法是,我想自动化制作测试套件,以使我不会忘记在测试套件中包含一些东西.

Here's what I want to do: I want to build a test suite that's organized into packages like tests.ui, tests.text, tests.fileio, etc. In each __init__.py in these packages, I want to make a test suite consisting of all the tests in all the modules in that package. Of course, getting all the tests can be done with unittest.TestLoader, but it seems that I have to add each module individually. So supposing that test.ui has editor_window_test.py and preview_window_test.py, I want the __init__.py to import these two files and get a list of the two module objects. The idea is that I want to automate making the test suites so that I can't forget to include something in the test suite.

执行此操作的最佳方法是什么?看起来这很容易,但是我什么也没找到.

What's the best way to do this? It seems like it would be an easy thing to do, but I'm not finding anything.

我正在使用Python 2.5 btw.

I'm using Python 2.5 btw.

推荐答案

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

Good answers here, but the best thing to do would be to use a 3rd party test discovery and runner like:

  • Nose (my favourite)
  • Trial (pretty nice, especially when testing async stuff)
  • py.test (less good, in my opinion)

它们都与普通的unittest.TestCase兼容,您将不必以任何方式修改测试,也不必使用其中的任何高级功能.只是用作套件发现.

They are all compatible with plain unittest.TestCase and you won't have to modify your tests in any way, neither would you have to use the advanced features in any of them. Just use as a suite discovery.

您是否有特定的原因想要重新创建这些库中的令人讨厌的内容?

Is there a specific reason you want to reinvent the nasty stuff in these libs?

这篇关于获取当前软件包中所有模块的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆