python代码写龙卷风_关于python:龙卷风应用程序的单元测试:如何改善错误消息的显示...

我正在使用unittest来测试具有多个处理程序的龙卷风应用程序,其中之一会引发异常。 如果我使用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,并且仅显示测试报告:

logging.disable(logging.CRITICAL)

你可以把它例如

创建的TestCase子类

测试选手

更多信息如何在Python Django中运行单元测试时禁用日志记录?

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

Python脚本从另一个测试结果生成JUnit报告

如何用鼻子测试输出coverage XML?

这是预期的行为。您的测试本身会断言返回码是HTTP 200,并且由于这是一个错误的形式断言,因此结果是"失败"而不是"错误"。您可以按照kwaranuk的答案中所述抑制日志,但是随后您将丢失有关实际导致HTTP 500错误的信息。

为什么您的代码到达断言而不是抛出?这是因为您的测试代码未调用HandlerWithError.get。您的测试代码使用AsyncHTTPTestCase类提供的HTTP客户端开始异步HTTP GET操作。 (有关详细信息,请检查该类的源代码。)事件循环运行,直到HandlerWithError.get通过本地主机套接字接收到请求,并使用HTTP 500在该套接字上响应。当HandlerWithError.get失败时,它不会引发测试功能中的异常,Google.com上的任何失败都会引发异常:它只会导致HTTP 500。

欢迎来到异步世界!没有简单的方法可以很好地将断言错误与HandlerWithError.get()的回溯关联起来。

感谢您的回答。 我已经编辑了我的问题,以弄清我困惑的原因。

编辑我的答案:您的测试函数不是直接调用引发异常的函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值