【问题标题】:How to run all tests with one shell command?如何使用一个 shell 命令运行所有测试?
【发布时间】:2017-10-17 03:10:39
【问题描述】:

假设我有几个模块的测试用例源自unittest.TestCase。所有这些模块都位于一个包中test

test/
   test_case1.py
   test_case2.py
   test_case3.py

我想使用 one shell 命令在 test 的所有模块中运行 all 测试。为了做到这一点,我添加了一个新模块test_all.py,它创建了一个包含所有测试用例的TestSuite,以及main

def make_suite():
  ... # add test cases explicitly one by one

if __name__ == "__main__":
  suite = make_suite()
  unittest.TextTestRunner().run(suite)

现在我想知道是否有一种方法可以运行 所有 test 中的测试用例,而无需创建 TestSuite

【问题讨论】:

  • 你反对使用nosetests这样的工具吗?
  • 是的,如果不是绝对必要的话。

标签: python unit-testing


【解决方案1】:

从 Python 2.7 开始,您可以执行以下操作:

python -m unittest discover <test_directory>

当然,您所有的测试目录都必须包含一个__init__.py 文件。

您还可以使用nose 之类的工具来运行您的测试。

另见:

【讨论】:

  • 非常感谢。 discover 解决方案可能是我需要的。
  • 太棒了。如果它最终工作,我希望你能接受答案
【解决方案2】:
for f in ./test/test_case*.py; do python $f; done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多