Django测试:查看回溯创建错误响应的位置 [英] Django Testing: See traceback where wrong Response gets created
问题描述
此模式来自Django文档:
This pattern is from the django docs:
class SimpleTest(unittest.TestCase):
def test_details(self):
client = Client()
response = client.get('/customer/details/')
self.assertEqual(response.status_code, 200)
来自: https://docs. djangoproject.com/zh-CN/1.8/topics/testing/tools/#default-test-client
如果测试失败,则错误消息不会有太大帮助.例如,如果status_code是302,那么我会看到302 != 200
.
If the test fails, the error message does not help very much. For example if the status_code is 302, then I see 302 != 200
.
现在的问题是:在哪里创建了错误的HTTPResponse?
The question is now: Where does the wrong HTTPResponse get created?
我想查看解释器的堆栈跟踪,其中创建了错误的HTTPResponse对象.
I would like to see the stacktrace of the interpreter where the wrong HTTPResponse object get created.
我阅读了 Django断言,但没有找到匹配方法.
I read the docs for the assertions of django but found no matching method.
更新
这是一个普遍的问题:如果断言失败,如何立即查看所需信息?由于这些断言(self.assertEqual(response.status_code, 200)
)很常见,因此我不想开始调试.
This is a general question: How to see the wanted information immediately if the assertion fails? Since these assertions (self.assertEqual(response.status_code, 200)
) are common, I don't want to start debugging.
更新2016年
我再次有同样的想法,发现当前答案并非100%容易.我写了一个新的答案,它有一个易于使用的解决方案(django Web客户端的子类):
I had the same idea again, found the current answer not 100% easy. I wrote a new answer, which has a simple to use solution (subclass of django web client): Django: assertEqual(response.status_code, 200): I want to see useful stack of functions calls
推荐答案
我认为可以通过创建TestCase
子类来实现,该子类可以猴子补丁django.http.response.HttpResponseBase.__init__()
记录堆栈跟踪并将其存储在Response
对象上,然后编写assertResponseCodeEquals(response, status_code=200)
方法,该方法将在失败时打印存储的堆栈跟踪信息,以显示Response
的创建位置.
I think it could be achieved by creating a TestCase
subclass that monkeypatches django.http.response.HttpResponseBase.__init__()
to record a stack trace and store it on the Response
object, then writing an assertResponseCodeEquals(response, status_code=200)
method that prints the stored stack trace on failure to show where the Response
was created.
我本人实际上可以真正使用一种解决方案,并且可以考虑实施它.
I could actually really use a solution for this myself, and might look at implementing it.
更新: 这是v1的实现,可以使用一些改进(例如,仅打印堆栈跟踪的相关行).
Update: Here's a v1 implementation, which could use some refinement (eg only printing relevant lines of the stack trace).
import mock
from traceback import extract_stack, format_list
from django.test.testcases import TestCase
from django.http.response import HttpResponseBase
orig_response_init = HttpResponseBase.__init__
def new_response_init(self, *args, **kwargs):
orig_response_init(self, *args, **kwargs)
self._init_stack = extract_stack()
class ResponseTracebackTestCase(TestCase):
@classmethod
def setUpClass(cls):
cls.patcher = mock.patch.object(HttpResponseBase, '__init__', new_response_init)
cls.patcher.start()
@classmethod
def tearDownClass(cls):
cls.patcher.stop()
def assertResponseCodeEquals(self, response, status_code=200):
self.assertEqual(response.status_code, status_code,
"Response code was '%s', expected '%s'" % (
response.status_code, status_code,
) + '\n' + ''.join(format_list(response._init_stack))
)
class MyTestCase(ResponseTracebackTestCase):
def test_index_page_returns_200(self):
response = self.client.get('/')
self.assertResponseCodeEquals(response, 200)
这篇关于Django测试:查看回溯创建错误响应的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!