gpt4 book ai didi

python - 单元测试 Tornado 应用程序: How to improve the display of error messages

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:35 24 4
gpt4 key购买 nike

我正在使用unittest来测试一个 Tornado 应用程序,该应用程序有多个处理程序,其中一个处理程序引发了异常。如果我使用 python test.py 运行以下测试代码:

# test.py

import unittest
import tornado.web
import tornado.testing

class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write('Hello World') # handler works correctly

class HandlerWithError(tornado.web.RequestHandler):
def get(self):
raise Exception('Boom') # handler raises an exception
self.write('Hello World')

def make_app():
return tornado.web.Application([
(r'/main/', MainHandler),
(r'/error/', HandlerWithError),
])

class TornadoTestCase(tornado.testing.AsyncHTTPTestCase):

def get_app(self):
return make_app()

def test_main_handler(self):
response = self.fetch('/main/')
self.assertEqual(response.code, 200) # test should pass

def test_handler_with_error(self):
response = self.fetch('/error/')
self.assertEqual(response.code, 200) # test should fail with error

if __name__ == '__main__':
unittest.main()

测试输出如下:

ERROR:tornado.application:Uncaught exception GET /error/ (127.0.0.1)                                                                                                                   
HTTPServerRequest(protocol='http', host='localhost:36590', method='GET', uri='/error/', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Connection': 'close', 'Host': 'localhost:3
6590', 'Accept-Encoding': 'gzip'})
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1332, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "test.py", line 13, in get
raise Exception('Boom') # handler raises an exception
Exception: Boom
ERROR:tornado.access:500 GET /error/ (127.0.0.1) 19.16ms
F.
======================================================================
FAIL: test_handler_with_error (__main__.TornadoTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/testing.py", line 118, in __call__
result = self.orig_method(*args, **kwargs)
File "test.py", line 33, in test_handler_with_error
self.assertEqual(response.code, 200) # test should fail with error
AssertionError: 500 != 200

----------------------------------------------------------------------
Ran 2 tests in 0.034s

FAILED (failures=1)

但是,我希望 unittest 在第二次测试中报告错误,而不是失败的断言。此外,“Boom”异常的回溯出现在单元测试测试报告之前,并且不包含对失败的测试函数的引用,这使得很难找到异常的来源。

有什么建议如何处理这种情况吗?

提前致谢!

编辑

令我意想不到的是,test_handler_with_error 实际上到达了 assertEqual 断言,而不是抛出错误。例如,以下代码不执行 self.assertEqual 语句,因此在测试输出中报告 ERROR 而不是 FAIL:

# simple_test.py
import unittest

def foo():
raise Exception('Boom')
return 'bar'

class SimpleTestCase(unittest.TestCase):
def test_failing_function(self):
result = foo()
self.assertEqual(result, 'bar')

if __name__ == '__main__':
unittest.main()

最佳答案

您可以禁用日志记录,这样只会显示测试报告:

logging.disable(logging.CRITICAL)

例如,您可以将其放入

  • 创建了 TestCase 子类
  • 测试运行者

更多信息How can I disable logging while running unit tests in Python Django?

请记住,CI/CD 系统实际上使用标准化报告,例如junit,然后以更易读/更优雅的方式呈现它 - 更多信息:

关于python - 单元测试 Tornado 应用程序: How to improve the display of error messages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42551015/

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