python unittest 模块,将失败记录到文件
python unittest module, log failures to file
我希望清理正常的 python 单元测试输出。我希望控制台输出仍然是
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok
test_fail (__main__.TestFail) ... ERROR
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
但对于失败测试,我想捕获详细的输出,并将其放入日志文件中。所以它不是与控制台输出内联...
======================================================================
FAIL: test_fail (__main__.TestFail)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line x
self.assertTrue(False)
AssertionError: False is not True
======================================================================
与任何调试级别的记录器输出一起被记录到文件中以供进一步调查。有没有办法让 unittest.testcase 中的记录器超载以执行我想要的操作?
我应该提一下我对python还是很陌生...
我最终能够通过使用 testResult 对象获得足够接近我想要的结果。从该对象中,我能够获得包含已通过、失败或有错误的不同测试数据的元组。然后它是一个简单的创建一个 "prettyPrint" 方法来获取这个对象并很好地打印出内容。
确切的配方是:
suite = unittest.TestLoader().loadTestsFromModule( className )
testResult = unittest.TextTestRunner(verbosity=3).run( suite )
希望这可以帮助其他任何想做类似事情的人。
可以通过向构造函数提供 stream
参数将 TextTestRunner
输出重定向到文件。稍后,在套件上使用 run()
将 return TextTestResult
,您可以漂亮地打印出来。像这样:
logs_filename = 'logs.txt'
def print_test_results_summary(result):
n_failed = len(result.failures) + len(result.unexpectedSuccesses)
n_crashed = len(result.errors)
n_succeeded = result.testsRun - n_failed - n_crashed
print(f'''See for details {logs_filename} file.
Results: Total: {result.testsRun}, Crashed: {n_crashed}, Failed: {n_failed}, Succeeded: {n_succeeded}''')
with open(logs_filename, 'w') as log_file:
suite = unittest.defaultTestLoader.loadTestsFromModule(className)
testResult = unittest.TextTestRunner(log_file, verbosity=3).run(suite)
print_test_results_summary(testResult)
我希望清理正常的 python 单元测试输出。我希望控制台输出仍然是
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok
test_fail (__main__.TestFail) ... ERROR
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
但对于失败测试,我想捕获详细的输出,并将其放入日志文件中。所以它不是与控制台输出内联...
======================================================================
FAIL: test_fail (__main__.TestFail)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line x
self.assertTrue(False)
AssertionError: False is not True
======================================================================
与任何调试级别的记录器输出一起被记录到文件中以供进一步调查。有没有办法让 unittest.testcase 中的记录器超载以执行我想要的操作?
我应该提一下我对python还是很陌生...
我最终能够通过使用 testResult 对象获得足够接近我想要的结果。从该对象中,我能够获得包含已通过、失败或有错误的不同测试数据的元组。然后它是一个简单的创建一个 "prettyPrint" 方法来获取这个对象并很好地打印出内容。
确切的配方是:
suite = unittest.TestLoader().loadTestsFromModule( className )
testResult = unittest.TextTestRunner(verbosity=3).run( suite )
希望这可以帮助其他任何想做类似事情的人。
可以通过向构造函数提供 stream
参数将 TextTestRunner
输出重定向到文件。稍后,在套件上使用 run()
将 return TextTestResult
,您可以漂亮地打印出来。像这样:
logs_filename = 'logs.txt'
def print_test_results_summary(result):
n_failed = len(result.failures) + len(result.unexpectedSuccesses)
n_crashed = len(result.errors)
n_succeeded = result.testsRun - n_failed - n_crashed
print(f'''See for details {logs_filename} file.
Results: Total: {result.testsRun}, Crashed: {n_crashed}, Failed: {n_failed}, Succeeded: {n_succeeded}''')
with open(logs_filename, 'w') as log_file:
suite = unittest.defaultTestLoader.loadTestsFromModule(className)
testResult = unittest.TextTestRunner(log_file, verbosity=3).run(suite)
print_test_results_summary(testResult)