简体   繁体   中英

Django Test is working with manage.py test app but not with manage.py test

I've written a TestCase for checking my view which needs a Teacher-Object.

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)

When I am running 'manage.py test app' it all works perfect.

If I'm running 'manage.py test' this Error disapears:

 self.model._meta.object_name
 students.gender.Gender.DoesNotExist: Gender matching query does not exist.

I am using these teacher objects also in other models-tests because of some foreign keys. So, is it possible or neccesary to reset the test_db before every test?

Here's the full 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.

The traceback shows that this line is failing:

gender_male = Gender.objects.get(pk=1)

You shouldn't usually hardcore primary keys in your code or tests. When you run all the tests with manage.py test , the object is being created with a different primary key, so get(pk=1) fails.

If you really need to hardcode the pk in your code, then you need to create the object with the correct pk in your test class:

gender = Gender.objects.create(pk=1, gender='Male')

However, a better fix would be to change the code so that it doesn't use a hardcoded pk.

gender_male = Gender.objects.get(gender='Male')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM