2011-09-16 65 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) 

希望你有一些想法!

相關問題