有没有办法直接在测试反馈中显示在 StaticLiveServerTestCase 期间服务器上发生的错误?也就是说,当某些服务器函数调用错误并且页面没有显示时,默认情况下测试执行不知道服务器错误。有没有办法将该输出传递到测试线程?
这些错误最好出现在与测试代码执行中直接出现的错误相同的位置。如果这不是(容易)可能的,那么快速查看这些服务器错误的下一个最佳方法是什么?
谢谢!
代码(按要求):
class TestFunctionalVisitor(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def tearDown(self):
self.browser.quit()
def test_visitor(self):
self.browser.get(self.live_server_url)
self.assertEqual(self.browser.title, "Something")
...
class Home(TemplateView):
template_name = 'home.html'
def get_context_data(self):
context = {}
MyModel = None
context['my_models'] = MyModel.objects.all()
return context
这已经被显着改变以使其简单和简短。但是当 MyModel
为 None
并尝试调用 objects.all()
时,服务器出现服务器 500 错误,但我得到的只是 "Something"not in self.browser.title
来自测试输出的错误,当我想在测试输出中看到 NoneType has no...
错误时。
最佳答案
要立即查看错误,请在 DEBUG 模式下运行测试:
from django.test.utils import override_settings
@override_settings(DEBUG=True)
class DjkSampleTestCase(StaticLiveServerTestCase):
# fixtures = ['club_app_phase01_2017-01-09_13-30-19-169537.json']
reset_sequences = True
但是还应该配置服务器端错误的日志记录,通过自定义 django.core.handlers.base.BaseHandler
类 handle_uncaught_exception()
方法实现或通过哨兵.
关于python - 在 Django StaticLiveServerTestCase 期间在测试输出中显示服务器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890784/