首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在一个目录中运行所有Python单元测试?

如何在一个目录中运行所有Python单元测试?
EN

Stack Overflow用户
提问于 2009-11-14 07:01:40
回答 17查看 292.9K关注 0票数 391

我有一个包含Python单元测试的目录。每个单元测试模块的形式都是test_*.py。我正在尝试创建一个名为all_test.py的文件,您可能已经猜到了,它将运行上述测试表单中的所有文件并返回结果。到目前为止,我已经尝试了两种方法;都失败了。我将展示这两种方法,我希望有人知道如何正确地执行此操作。

对于我的第一次valiant尝试,我想“如果我只导入文件中的所有测试模块,然后将其命名为unittest.main() doodad,它将会工作,对吗?”事实证明我错了。

代码语言:javascript
复制
import glob
import unittest

testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]

if __name__ == "__main__":
     unittest.main()

这不起作用,我得到的结果是:

代码语言:javascript
复制
$ python all_test.py 

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

对于我的第二次尝试,我想,好吧,也许我会尝试以一种更“手动”的方式来做整个测试工作。因此,我尝试在下面这样做:

代码语言:javascript
复制
import glob
import unittest

testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]
[__import__(str) for str in module_strings]
suites = [unittest.TestLoader().loadTestsFromName(str) for str in module_strings]
[testSuite.addTest(suite) for suite in suites]
print testSuite 

result = unittest.TestResult()
testSuite.run(result)
print result

#Ok, at this point I have a result
#How do I display it as the normal unit test command line output?
if __name__ == "__main__":
    unittest.main()

这也不起作用,但看起来很接近!

代码语言:javascript
复制
$ python all_test.py 
<unittest.TestSuite tests=[<unittest.TestSuite tests=[<unittest.TestSuite tests=[<test_main.TestMain testMethod=test_respondes_to_get>]>]>]>
<unittest.TestResult run=1 errors=0 failures=0>

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

我似乎有一个套件,我可以执行结果。我有点担心它说我只有run=1,看起来应该是run=2,但它是进步。但是我如何将结果传递给main并显示出来呢?或者我如何让它正常工作,这样我就可以运行这个文件,并且在这样做的时候,运行这个目录中的所有单元测试?

EN

回答 17

Stack Overflow用户

回答已采纳

发布于 2013-03-26 14:09:41

在Python2.7和更高版本中,您不必编写新代码或使用第三方工具来完成此任务;通过命令行执行递归测试是内置的。在您的测试目录中放置一个__init__.py,然后:

代码语言:javascript
复制
python -m unittest discover <test_directory>
# or
python -m unittest discover -s <directory> -p '*_test.py'

您可以在python 2.7python 3.x单元测试文档中阅读更多内容。

2021年更新:

许多现代python项目使用更高级的工具,如pytest。例如,下拉matplotlibscikit-learn,您将看到它们都在使用它。

了解这些较新的工具很重要,因为当您有超过7000个测试时,您需要:

使用更高级的方法来汇总通过、跳过、警告和错误的内容,轻松查看它们是如何完成的,因为它是running

  • total
  • ways to failed
  • percent a test report

etc<代码>F220

票数 588
EN

Stack Overflow用户

发布于 2017-05-02 16:27:43

在Python3中,如果您使用的是unittest.TestCase

测试文件你必须有一个空的(否则)在你的测试目录中的测试文件(必须命名为test/)

  • Your
  • __init__.py test test/中的测试文件必须与模式test_*.py匹配。它们可以在test/下的子目录中,这些子目录可以命名为任何名称。

然后,您可以使用以下命令运行所有测试:

代码语言:javascript
复制
python -m unittest

完成了!一个少于100行的解决方案。希望另一位python初学者可以找到它来节省时间。

票数 177
EN

Stack Overflow用户

发布于 2009-11-14 07:12:06

你可以使用一个测试运行器来为你做这件事。例如,nose是一个非常好的例子。运行时,它将在当前树中查找测试并运行它们。

更新:

这是我做鼻子前的时候的一些代码。您可能不需要模块名称的显式列表,但其余的可能对您有用。

代码语言:javascript
复制
testmodules = [
    'cogapp.test_makefiles',
    'cogapp.test_whiteutils',
    'cogapp.test_cogapp',
    ]

suite = unittest.TestSuite()

for t in testmodules:
    try:
        # If the module defines a suite() function, call it to get the suite.
        mod = __import__(t, globals(), locals(), ['suite'])
        suitefn = getattr(mod, 'suite')
        suite.addTest(suitefn())
    except (ImportError, AttributeError):
        # else, just load all the test cases from the module.
        suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t))

unittest.TextTestRunner().run(suite)
票数 110
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1732438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档