Я написал TestCase для проверки моего представления, для которого нужен объект-учитель.
class ShowTeacherViewTest(TestCase):
@classmethod
def setUpTestData(cls):
gender = Gender.objects.create(gender='Male')
gender_id = gender.id
Teacher.objects.create(
gender_id=gender_id,
first_name='Maria',
last_name='Santana',)
def test_view_uses_correct_template(self):
teacher = Teacher.objects.first().id
response = self.client.get(reverse('get_student'), {'teacher': teacher})
self.assertEqual(response.status_code, 200)
Когда я запускаю 'manage.py test app', все работает отлично.
Если я запускаю 'manage.py test', эта ошибка исчезает:
self.model._meta.object_name
students.gender.Gender.DoesNotExist: Gender matching query does not exist.
Я использую эти объекты учителя также в других моделях-тестах из-за некоторых внешних ключей.Итак, возможно или необходимо сбрасывать test_db перед каждым тестом?
Вот полный Traceback:
Traceback (most recent call last):
File "/Users/user/django/venv/teaching/tests/test_get_students.py", line 157, in test_view_uses_correct_template
response = self.client.get(reverse('get_student'), {'student': student})
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/test/client.py", line 527, in get
response = super().get(path, data=data, secure=secure, **extra)
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/test/client.py", line 339, in get
**extra,
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/test/client.py", line 414, in generic
return self.request(**r)
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/test/client.py", line 495, in request
raise exc_value
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/user/django/venv/teaching/get_students.py", line 55, in get_student
image = get_alibi_pic(image, gender, student)
File "/Users/user/django/venv/teaching/get_students.py", line 27, in get_alibi_pic
gender_male = Gender.objects.get(pk=1)
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/user/anaconda3/envs/venv/lib/python3.7/site-packages/django/db/models/query.py", line 399, in get
self.model._meta.object_name
students.gender.Gender.DoesNotExist: Gender matching query does not exist.