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

我有一个包含我的Python单元测试的目录。 每个单元测试模块的形式为test _ *。py 。 我试图创建一个名为all_test.py的文件,你会猜到它会运行上述测试表单中的所有文件并返回结果。 到目前为止,我尝试了两种方法; 都失败了。 我将展示这两种方法,并希望有人知道如何正确地做到这一点。

对于我第一次勇敢的尝试,我认为“如果我只是将所有测试模块导入到文件中,然后调用这个unittest.main()装饰物,它会起作用,对吧?” 那么,事实证明我错了。

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

这没有奏效,我得到的结果是:

$ python all_test.py 

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

OK

对于我的第二次尝试,虽然我确定,也许我会尝试以更“手动”的方式完成整个测试。 所以我试图做到以下几点:

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

这也不起作用,但它似乎如此接近!

$ 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 ,但这是进步。 但是,我如何传递和显示结果为主? 或者我如何基本得到它的工作,所以我可以运行这个文件,这样做,运行在这个目录中的所有单元测试?


你可以使用一个测试跑步者为你做这个。 例如鼻子非常好。 运行时,它会在当前树中找到测试并运行它们。

更新:

这是我鼻子前几天的一些代码。 你可能不需要明确的模块名称列表,但其余的可能对你有用。

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)

在Python 2.7及更高版本中,您不必编写新代码或使用第三方工具来执行此操作; 通过命令行执行递归测试是内置的。

python -m unittest discover <test_directory>
# or
python -m unittest discover -s <directory> -p '*_test.py'

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


那么通过研究上面的代码(特别是使用TextTestRunnerdefaultTestLoader ),我能够变得非常接近。 最终我通过将所有测试套件传递给一个套件构造函数来修复我的代码,而不是“手动”添加它们,这解决了我的其他问题。 所以这是我的解决方案。

import glob
import unittest

test_files = glob.glob('test_*.py')
module_strings = [test_file[0:len(test_file)-3] for test_file in test_files]
suites = [unittest.defaultTestLoader.loadTestsFromName(test_file) for test_file in module_strings]
test_suite = unittest.TestSuite(suites)
test_runner = unittest.TextTestRunner().run(test_suite)

是的,使用鼻子可能比使用鼻子更容易,但那不仅仅是重点。

链接地址: http://www.djcxy.com/p/54831.html

上一篇: How do I run all Python unit tests in a directory?

下一篇: Mixins vs composition in scala