Sto scrivendo alcuni test per la mia app django. A mio avviso, reindirizza ad un altro URL utilizzando 'HttpResponseRedirect'. Come posso verificarlo?Django: Come eseguire il test di 'HttpResponsePermanentRedirect'
risposta
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.
L'ultima riga genera un errore in Django 1.4: 'AttributeError: 'HttpResponseRedirect' l'oggetto non ha attributo 'META'' –
Yup, modificato in 1.4 sembra. Invece dovresti confrontare 'response.get ('location')' con l'url di redire desiderato. – aychedee
L'utilizzo di 1.4.5 e META non è disponibile in risposta ??? – simi
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.
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). –
@ 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
Ciò ha più senso. Grazie. –
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)
- 1. Come eseguire il debug dei test delle unità Django?
- 2. Come eseguire il debug dei test falliti in Django?
- 3. Come posso eseguire i test a livello di progetto Django?
- 4. Come eseguire il test dell'unità di concorrenza?
- 5. Come eseguire i test per tutte le mie app Django?
- 6. Come eseguire il "bulk update" con Django?
- 7. Come eseguire il progetto clonato Django?
- 8. Come eseguire il test dell'unità in pyCharm
- 9. Test di Django allauth
- 10. Come distruggere automaticamente il database di test di django
- 11. Django: test di file statici
- 12. Il database di test di Django sembra vuoto mentre il test è in esecuzione
- 13. Posso eseguire i test django (manage.py) da una directory diversa?
- 14. COME eseguire il test delle variabili di sessione in PHP?
- 15. Come eseguire il test di integrazione per un progetto WCF?
- 16. Come eseguire il test di integrazione ASP.NET MVC utilizzando selenio
- 17. Come eseguire l'override di log4j.properties durante il test?
- 18. Come posso eseguire il test prima di generare l'apk firmato?
- 19. Come eseguire il test di carico per le prese Web
- 20. Come eseguire un'istanza di ricerca elastica incorporata per il test
- 21. Come eseguire "pacchetto" prima di "test"
- 22. Esecuzione del test di Django con setup.py test e tox
- 23. Come eseguire i test di integrazione?
- 24. Come eseguire il server jetty per il test java junit
- 25. Come avviare test per l'app riutilizzabile django?
- 26. Come eseguire i test di integrazione?
- 27. Fitnesse: come eseguire test specifici su più suite di test
- 28. Ordine di test Django TestCase
- 29. django non può rilasciare il DB di test tramite pgbouncer
- 30. Test dei flussi di lavoro in Django
Potrebbe elaborare più? Cosa intendi esattamente per testarlo? – Babu