2012-07-27 14 views

risposta

14
from django.http import HttpResponsePermanentRedirect 
from django.test.client import Client 

class MyTestClass(unittest.TestCase): 

    def test_my_method(self): 

     client = Client() 
     response = client.post('/some_url/') 

     self.assertEqual(response.status_code, 301) 
     self.assertTrue(isinstance(response, HttpResponsePermanentRedirect)) 
     self.assertEqual(response.META['HTTP_LOCATION'], '/url_we_expect_to_be_redirected_to/') 

Ci sono altri attributi della risposta che potrebbero essere interessanti da testare. Se non siete sicuri che cosa è l'oggetto allora si può sempre fare un EDIT

print dir(response) 

per le versioni attuali di Django

E 'un po' più semplice ora, basta fare:

self.assertEqual(response.get('location'), '/url/we/expect') 

Ti suggerisco inoltre di utilizzare il reverse per cercare l'url che ti aspetti da un nome, se è comunque un URL nella tua app.

+1

L'ultima riga genera un errore in Django 1.4: 'AttributeError: 'HttpResponseRedirect' l'oggetto non ha attributo 'META'' –

+2

Yup, modificato in 1.4 sembra. Invece dovresti confrontare 'response.get ('location')' con l'url di redire desiderato. – aychedee

+0

L'utilizzo di 1.4.5 e META non è disponibile in risposta ??? – simi

17

La classe Django TestCase ha un metodo assertRedirects che è possibile utilizzare.

from django.test import TestCase 

class MyTestCase(TestCase): 

    def test_my_redirect(self): 
     """Tests that /my-url/ permanently redirects to /next-url/""" 
     response = self.client.get('/my-url/') 
     self.assertRedirects(response, '/next-url/', status_code=301) 

Il codice di stato 301 verifica che si tratti di un reindirizzamento permanente.

+0

Funziona per chiunque usi Django '<= 1.5.2'? Sembra che abbia colpito [questo bug] (https://bitbucket.org/kmike/django-webtest/issue/8/assertredirects-response-code-was-302). –

+0

@ JonasG.Drange stai collegando un bug report per la versione 1.5.2 dell'app django-webtest, non Django stesso. Nella mia risposta originale, ho suggerito di utilizzare 'follow = False' nella richiesta get per impedire al client di seguire il reindirizzamento. Non era corretto, quindi l'ho rimosso. Il metodo 'assertRedirect' controlla sempre la pagina di stato, come documentato. C'è un ticket [# 20919] (https://code.djangoproject.com/ticket/20919) per consentire a 'assertRedirects' di non caricare la pagina di destinazione. – Alasdair

+0

Ciò ha più senso. Grazie. –

2

In Django 1.6, è possibile utilizzare (non raccomandato):

from django.test import TestCase 
from django.http import HttpResponsePermanentRedirect 

class YourTest(TestCase): 
    def test_my_redirect(self): 
     response = self.client.get('/url-you-want-to-test/') 
     self.assertEqual(response.status_code, 301)# premant 301, temporary 302 
     self.assertTrue(isinstance(response, HttpResponsePermanentRedirect)) 
     self.assertEqual(response.get('location'), 'http://testserver/redirect-url/') 

invece, segue è più potente e conciso e senza necessità http://testserver/

from django.test import TestCase 

class YourTest(TestCase): 
    def test1(self): 
     response = self.client.get('/url-you-want-to-test/') 
     self.assertRedirects(
      response, '/redirect-url/',status_code=301,target_status_code=200) 
Problemi correlati