Python 2.7 使用python unittest的多个测试文件的一个摘要

Python 2.7 使用python unittest的多个测试文件的一个摘要,python-2.7,unit-testing,Python 2.7,Unit Testing,我想为我的python项目进行自动测试,但我不确定使用unittest模块的正确方法 我的所有测试文件当前都位于一个文件夹中,并具有以下格式: import unittest class SampleTest(unittest.TestCase): def testMethod(self): # Assertion here if __name__ == "__main__": unittest.main() 然后我就跑 find ./tests -name "

我想为我的python项目进行自动测试,但我不确定使用
unittest
模块的正确方法

我的所有测试文件当前都位于一个文件夹中,并具有以下格式:

import unittest
class SampleTest(unittest.TestCase):
    def testMethod(self):
        # Assertion here
if __name__ == "__main__":
    unittest.main()
然后我就跑

find ./tests -name "*_test.py" -exec python {} \;
当有三个测试文件时,它输出

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
它为每个测试文件打印了一个摘要。所以问题是我能做些什么使它只打印一个测试摘要,例如
在0.001s中运行了5个测试

提前谢谢


我不想安装任何其他模块

您多次调用Python,并且每个进程都不知道其他模块。您需要运行一次Python并使用unittest发现机制

在shell中运行:

python -m unittest discover
根据您的项目结构和命名约定,您可能需要调整发现参数,例如更改
--pattern
选项,如帮助中所述:

Usage: python -m unittest discover [options]

Options:
  -h, --help            show this help message and exit
  -v, --verbose         Verbose output
  -f, --failfast        Stop on first fail or error
  -c, --catch           Catch Ctrl-C and display results so far
  -b, --buffer          Buffer stdout and stderr during tests
  -s START, --start-directory=START
                        Directory to start discovery ('.' default)
  -p PATTERN, --pattern=PATTERN
                        Pattern to match tests ('test*.py' default)
  -t TOP, --top-level-directory=TOP
                        Top level directory of project (defaults to start
                        directory)
虽然你说我不想安装任何其他模块,但我还是建议使用另一个测试运行程序。有相当多的人在那里,或举几个例子