gpt4 book ai didi

python - 运行 python unittest 时显示进度?

转载 作者:太空狗 更新时间:2023-10-30 02:33:51 24 4
gpt4 key购买 nike

我有一个非常大的 TestSuite,我使用 python unittest 框架中的 TextTestRunner 运行它。不幸的是,我不知道在测试运行时已经完成了多少测试。

基本上我想转换这个输出:

test_choice (__main__.TestSequenceFunctions) ... ok
test_sample (__main__.TestSequenceFunctions) ... ok
test_shuffle (__main__.TestSequenceFunctions) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.110s

OK

[1/3] test_choice (__main__.TestSequenceFunctions) ... ok
[2/3] test_sample (__main__.TestSequenceFunctions) ... ok
[3/3] test_shuffle (__main__.TestSequenceFunctions) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.110s

OK

我是否必须继承 TextTestRunner 才能实现此目的?如果是,如何实现?

注意:我知道 nose 和可用的插件,但它不太适合我的应用程序,我想避免依赖。

编辑 为什么我想避开 Nose :

我的应用程序基本上是用于测试的附加框架。它选择正确的测试用例,为它们提供库函数并多次执行测试以进行长期测试。 (测试针对外部机器运行)

下面是我现在运行测试的方式:

# do all sort of preperations
[...]
test_suite = TestSuite()

repeatitions = 100
tests = get_tests()
for i in range(0, repeatitions):
test_suite.addTests(tests)
TextTestRunner(verbosity=2).run(test_suite)

我的 nose 问题是,它旨在从文件系统中发现测试,而我无法找到关于如何直接从 python 在特定 TestSuite 上运行它的清晰文档。

最佳答案

聚会迟到了,但希望这能帮助像我一样来到这里寻找同一问题答案的其他人:

这是您可以子类化 TextTestRunnerTextTestResult 以获得所需结果的一种方法:

import unittest
import unittest.runner
import itertools
import collections

class CustomTextTestResult(unittest.runner.TextTestResult):
"""Extension of TextTestResult to support numbering test cases"""

def __init__(self, stream, descriptions, verbosity):
"""Initializes the test number generator, then calls super impl"""

self.test_numbers = itertools.count(1)

return super(CustomTextTestResult, self).__init__(stream, descriptions, verbosity)

def startTest(self, test):
"""Writes the test number to the stream if showAll is set, then calls super impl"""

if self.showAll:
progress = '[{0}/{1}] '.format(next(self.test_numbers), self.test_case_count)
self.stream.write(progress)

# Also store the progress in the test itself, so that if it errors,
# it can be written to the exception information by our overridden
# _exec_info_to_string method:
test.progress_index = progress

return super(CustomTextTestResult, self).startTest(test)

def _exc_info_to_string(self, err, test):
"""Gets an exception info string from super, and prepends 'Test Number' line"""

info = super(CustomTextTestResult, self)._exc_info_to_string(err, test)

if self.showAll:
info = 'Test number: {index}\n{info}'.format(
index=test.progress_index,
info=info
)

return info


class CustomTextTestRunner(unittest.runner.TextTestRunner):
"""Extension of TextTestRunner to support numbering test cases"""

resultclass = CustomTextTestResult

def run(self, test):
"""Stores the total count of test cases, then calls super impl"""

self.test_case_count = test.countTestCases()
return super(CustomTextTestRunner, self).run(test)

def _makeResult(self):
"""Creates and returns a result instance that knows the count of test cases"""

result = super(CustomTextTestRunner, self)._makeResult()
result.test_case_count = self.test_case_count
return result


class TestSequenceFunctions(unittest.TestCase):
"""Dummy test case to illustrate usage"""

fail_1 = 0
fail_2 = 0

def test_choice(self):
pass

def test_sample(self):
self.fail_1 += 1
if self.fail_1 == 2:
raise Exception()

def test_shuffle(self):
self.fail_2 += 1
if self.fail_2 == 3:
raise Exception()


def get_tests():
test_funcs = ['test_choice', 'test_sample', 'test_shuffle']
return [TestSequenceFunctions(func) for func in test_funcs]


if __name__ == '__main__':
test_suite = unittest.TestSuite()

repetitions = 3
tests = get_tests()
for __ in xrange(0, repetitions):
test_suite.addTests(tests)

CustomTextTestRunner(verbosity=2).run(test_suite)

运行上面的代码会产生以下输出:

>>> ================================ RESTART ================================
>>>
[1/9] test_choice (__main__.TestSequenceFunctions) ... ok
[2/9] test_sample (__main__.TestSequenceFunctions) ... ok
[3/9] test_shuffle (__main__.TestSequenceFunctions) ... ok
[4/9] test_choice (__main__.TestSequenceFunctions) ... ok
[5/9] test_sample (__main__.TestSequenceFunctions) ... ERROR
[6/9] test_shuffle (__main__.TestSequenceFunctions) ... ok
[7/9] test_choice (__main__.TestSequenceFunctions) ... ok
[8/9] test_sample (__main__.TestSequenceFunctions) ... ok
[9/9] test_shuffle (__main__.TestSequenceFunctions) ... ERROR

======================================================================
ERROR: test_sample (__main__.TestSequenceFunctions)
----------------------------------------------------------------------
Test number: [5/9]
Traceback (most recent call last):
File "stackoverflow.py", line 75, in test_sample
raise Exception()
Exception

======================================================================
ERROR: test_shuffle (__main__.TestSequenceFunctions)
----------------------------------------------------------------------
Test number: [9/9]
Traceback (most recent call last):
File "stackoverflow.py", line 80, in test_shuffle
raise Exception()
Exception

----------------------------------------------------------------------
Ran 9 tests in 0.042s

FAILED (errors=2)
>>>

关于python - 运行 python unittest 时显示进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com