python - 在 Django StaticLiveServerTestCase 期间在测试输出中显示服务器错误?

标签 python django python-3.x testing functional-testing

有没有办法直接在测试反馈中显示在 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

这已经被显着改变以使其简单和简短。但是当 MyModelNone 并尝试调用 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.BaseHandlerhandle_uncaught_exception() 方法实现或通过哨兵.

关于python - 在 Django StaticLiveServerTestCase 期间在测试输出中显示服务器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890784/

相关文章:

python - 在无法容纳内存的大文件中查找字符串的出现

python - firebase-admin python 发送密码重置邮件

python - Django 通过连接进行排序的效率 - 我可以强制使用子查询吗?

python - 使用 linux 删除 python 安装文件后如何向管理员发送电子邮件警报?

python-3.x - pytest-ordering 相对顺序不起作用?

python - Python 中的参数和参数如何工作?

python - 数据库未使用 django 表单更新

python - 比较 Django 模型中的旧字段和更新字段

python - 使用 super() 和使用 self 从父类调用方法有什么区别?

python - 在 sklearn 中使用决策树回归和交叉验证