동일한 테스트 케이스 클래스에서 다른 테스트를 통과하는 단정에서 실패한 단위 테스트가 있습니다.장고 테스트 클라이언트 응답에 빈 컨텍스트가 있음
def test_home(self):
c = Client()
resp = c.get('/')
self.assertEqual(resp.status_code, 200)
self.assertTrue('a_formset' in resp.context)
여기 실패한 테스트입니다 :
여기 통과 테스트의 두 번째 테스트에서
def test_number_initial_number_of_forms(self):
c = Client()
resp = c.get('/')
self.assertEqual(resp.context['a_formset'].total_form_count(), 1)
, 나는 오류 TypeError: 'NoneType' object has no attribute '__getitem__'
를 얻을. 내가
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)
로 두 번째 테스트를 실행하면
나는 오류 TypeError: argument of type 'NoneType' is not iterable
를 얻을. 두 번째 테스트에서 print 문을 통해 response.content에 예상되는 페이지가 포함되어 있고 상태 코드가 올바른지 그리고 템플릿이 올바른지 확인했습니다. 그러나 두 번째 테스트에서 응답의 컨텍스트는 일관되게 None
입니다.
표준 "python manage.py test ..."인터페이스를 통해 장고 테스트를 실행 중이므로 "context is empty from the shell"문제가 발생한다고 생각하지 않습니다.
이 경우 어떻게됩니까?
편집 :
나는 각각의 테스트에 print type(resp.context['a_formset'])
를 추가하는 경우, 작동 테스트를 위해 내가 <class 'django.forms.formsets.AFormFormSet'>
를 얻을. 작동하지 않는 테스트의 경우 다시 TypeError: 'NoneType' object has no attribute '__getitem__'
이됩니다.
'a_form'은 (는) formset입니까? – sneawo
@sneawo 네, formset입니다. –
작업 및 비 작동 테스트에서'print type (resp.context [ 'a_formset'])'행을 임시로 추가하십시오. 당신은 당신이 기대하는 것을 얻지 못할 수도 있습니다. –