如何在目录中运行所有 Python 单元测试? [英] How do I run all Python unit tests in a directory?

查看:39
本文介绍了如何在目录中运行所有 Python 单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a directory that contains my Python unit tests. Each unit test module is of the form test_*.py. I am attempting to make a file called all_test.py that will, you guessed it, run all files in the aforementioned test form and return the result. I have tried two methods so far; both have failed. I will show the two methods, and I hope someone out there knows how to actually do this correctly.

对于我的第一次勇敢尝试,我想如果我只是在文件中导入我所有的测试模块,然后调用这个 unittest.main() 小玩意儿,它会起作用,对吧?"好吧,事实证明我错了.

For my first valiant attempt, I thought "If I just import all my testing modules in the file, and then call this unittest.main() doodad, it will work, right?" Well, turns out I was wrong.

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

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

This did not work, the result I got was:

$ python all_test.py 

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

OK

对于我的第二次尝试,好吧,也许我会尝试以更手动"的方式完成整个测试.所以我尝试在下面这样做:

For my second try, I though, ok, maybe I will try to do this whole testing thing in a more "manual" fashion. So I attempted to do that below:

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

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

This also did not work, but it seems so close!

$ 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 呢?或者我如何基本上让它工作,这样我就可以运行这个文件,然后运行这个目录中的所有单元测试?

I seem to have a suite of some sort, and I can execute the result. I am a little concerned about the fact that it says I have only run=1, seems like that should be run=2, but it is progress. But how do I pass and display the result to main? Or how do I basically get it working so I can just run this file, and in doing so, run all the unit tests in this directory?

推荐答案

使用 Python 2.7 及更高版本,您无需编写新代码或使用第三方工具来执行此操作;通过命令行执行递归测试是内置的.将 __init__.py 放在您的测试目录中,然后:

With Python 2.7 and higher you don't have to write new code or use third-party tools to do this; recursive test execution via the command line is built-in. Put an __init__.py in your test directory and:

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

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

You can read more in the python 2.7 or python 3.x unittest documentation.

2021 年更新:

许多现代 Python 项目使用更高级的工具,例如 pytest.例如,下拉 matplotlibscikit-learn 你会看到他们都在使用它.

Lots of modern python projects use more advanced tools like pytest. For example, pull down matplotlib or scikit-learn and you will see they both use it.

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

It is important to know about these newer tools because when you have more than 7000 tests you need:

  • 更高级的方法来总结通过、跳过、警告、错误的内容
  • 查看失败原因的简单方法
  • 运行完成的百分比
  • 总运行时间
  • 生成测试报告的方法
  • 等等等等

这篇关于如何在目录中运行所有 Python 单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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