Ho un test unitario che ha esito negativo in un'asserzione che passa in un altro test nella stessa classe del test case.La risposta del client di prova dell'unità Django ha un contesto vuoto
Ecco il test di passaggio:
def test_home(self):
c = Client()
resp = c.get('/')
self.assertEqual(resp.status_code, 200)
self.assertTrue('a_formset' in resp.context)
Ecco il test in mancanza:
def test_number_initial_number_of_forms(self):
c = Client()
resp = c.get('/')
self.assertEqual(resp.context['a_formset'].total_form_count(), 1)
Nella seconda prova, ottengo l'errore TypeError: 'NoneType' object has no attribute '__getitem__'
.
Se eseguo il secondo test come
def test_number_initial_number_of_forms(self):
c = Client()
resp = c.get('/')
self.assertTrue('a_formset' in resp.context)
self.assertEqual(resp.context['a_formset'].total_form_count(), 1)
ottengo l'errore TypeError: argument of type 'NoneType' is not iterable
. Ho confermato tramite dichiarazioni di stampa nel secondo test che response.content contiene la pagina che mi aspetto di ottenere, che il codice di stato è corretto e che il modello è corretto. Ma il contesto della risposta è sempre None
nel secondo test.
Sto eseguendo i miei test di unità Django tramite l'interfaccia standard "python manage.py test ...", quindi non credo di trovarmi nel problema "context is empty from the shell".
Cosa sta succedendo?
Edit:
Se aggiungo print type(resp.context['a_formset'])
ad ogni test, per il test di lavoro ottengo <class 'django.forms.formsets.AFormFormSet'>
. Per il test non funzionante, ottengo di nuovo TypeError: 'NoneType' object has no attribute '__getitem__'
.
'a_form' è un formset? – sneawo
@sneawo Sì, è un formset. –
In entrambi i test di lavoro e non lavorativi, aggiungi temporaneamente il tipo di stampa 'linea (resp.context ['a_formset'])'. Potresti non ottenere ciò che ti aspetti. –