如何使django liveservertestcase日志请求



django具有记录每个HTTP请求的功能。例如:

INFO 2017-08-31 11:52:40,725 arnaud basehttp "GET /design/projects/ HTTP/1.1" 200 3852

不幸的是,运行LiveServerTestCase时似乎已禁用。为了简化我的端到端测试的调试,我希望此功能保持激活。

事实证明,此行为似乎不可配置。我找不到有关该主题的任何文档,并诉诸于Django内部潜水。

解决方案

免责声明:这是非正式的,因此脆弱)

覆盖LiveServerTestCaseserver_thread_class属性

from django.core.servers.basehttp import ThreadedWSGIServer, WSGIRequestHandler
from django.test.testcases import LiveServerTestCase, LiveServerThread
class End2End(LiveServerTestCase):
    class VersboseLiveServerThread(LiveServerThread):
        def _create_server(self):
            return ThreadedWSGIServer((self.host, self.port), WSGIRequestHandler, allow_reuse_address=False)
    server_thread_class = VersboseLiveServerThread

更多详细信息

这个解决方案以及无法正确配置该行为的感觉,源于观察LiveServerTestCase的构建方式(Django 1.11):

class LiveServerTestCase(TransactionTestCase):
    # [...]
    server_thread_class = LiveServerThread
class LiveServerThread(threading.Thread):
    # [...]
    def _create_server(self):
        # an extension point returning the WSGIRequestHandler would have been nice :)
        return ThreadedWSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)
class QuietWSGIRequestHandler(WSGIRequestHandler):
    def log_message(*args):
        pass

最新更新