2011-09-16 121 views
30

我目前正在运行一些Django测试,默认情况下它看起来是DEBUG=False。有没有办法运行一个特定的测试,我可以在命令行或代码中设置DEBUG=True运行Django测试时,如何将DEBUG设置为True?

+0

根据https://docs.djangoproject.com/en/1.3/topics/testing/#other-test-conditions,测试集“DEBUG = False”。我正在寻找一种方法来绕过这一点。即使我在'settings.py'中传递'DEBUG = True',它将在运行测试时恢复为'False'。 –

+4

没关系,我可以在'setUp'下设置'settings.DEBUG = True' –

回答

-2

运行单元测试时,你看不到的DEBUG=True结果。页面不显示任何地方。没有浏览器。

更改DEBUG没有任何影响,因为网页(与调试输出)是不可见的任何地方。

如果你想看到相关的失败的单元测试调试网页,然后做到这一点。

  1. 删除您的开发数据库。

  2. 重新运行syncdb建立一个空的开发数据库。

  3. 运行各种loaddata脚本重建灯具在开发数据库测试。

  4. 运行服务器并浏览页面。

现在您可以看到调试输出。

+1

我意识到这是一个较旧的帖子,但想要'DEBUG = True'的一个很好的理由是看到来自tastypie REST API的消息,只有'DEBUG = True'和'TASTYPIE_FULL_DEBUG = True'时才显示。 – Tim

+1

另外,当使用硒时,您确实显示了网页。有人可能希望看到实际的错误消息,如果在构建测试时某些工作不正确。 –

+2

设置DEBUG = True还允许您从''django.db.connection.queries''输出生成的SQL,这可以有助于制定测试以涵盖奇怪的边界情况。 –

67

对于测试用例中一个特定的测试,你可以使用override_settings装饰:

from django.test.utils import override_settings 
from django.conf import settings 
class TestSomething(TestCase): 
    @override_settings(DEBUG=True) 
    def test_debug(self): 
     assert settings.DEBUG 
+0

截至Django 1.8,selnium 2.48.0这为我工作。 – Chuck

+0

您也可以覆盖整个测试类的设置。请参阅https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.override_settings – Scott

11

接受的答案并没有为我工作。我使用Selenium进行测试,并且设置​​会使测试浏览器始终在每个页面上显示404错误。并且DEBUG=False不显示异常回溯。所以我找到了一个解决方法。

这个想法是模仿DEBUG=True行为,使用自定义500处理程序和内置的django 500错误处理程序。

  1. 一下添加到myapp.views:

    import sys 
    from django import http 
    from django.views.debug import ExceptionReporter 
    
    def show_server_error(request): 
        """ 
        500 error handler to show Django default 500 template 
        with nice error information and traceback. 
        Useful in testing, if you can't set DEBUG=True. 
    
        Templates: `500.html` 
        Context: sys.exc_info() results 
        """ 
        exc_type, exc_value, exc_traceback = sys.exc_info() 
        error = ExceptionReporter(request, exc_type, exc_value, exc_traceback) 
        return http.HttpResponseServerError(error.get_traceback_html()) 
    
  2. urls.py:

    from django.conf import settings 
    
    if settings.TESTING_MODE: 
        # enable this handler only for testing, 
        # so that if DEBUG=False and we're not testing, 
        # the default handler is used 
        handler500 = 'myapp.views.show_server_error' 
    
  3. settings.py:

    # detect testing mode 
    import sys 
    TESTING_MODE = 'test' in sys.argv 
    

现在,如果您的Selenium测试中有任何一个测试遇到500错误,您将看到一个带有回溯和一切的好错误页面。如果运行正常的非测试环境,则使用默认的500处理程序。

灵感来自:

0

好吧,让我们说,我想要写错误测试用例的测试针对的网址是: -

urls.py

if settings.DEBUG: 
    urlpatterns += [ 
     url(r'^404/$', page_not_found_view), 
     url(r'^500/$', my_custom_error_view), 
     url(r'^400/$', bad_request_view), 
     url(r'^403/$', permission_denied_view), 
    ] 

test_urls.py: -

from django.conf import settings 

class ErroCodeUrl(TestCase): 

    def setUp(self): 
     settings.DEBUG = True 

    def test_400_error(self): 
     response = self.client.get('/400/') 
     self.assertEqual(response.status_code, 500) 

希望你有一些想法!

相关问题