3

TL;DR:

I don't wanna use @skipIfCustomUser What can I do?


Django allows a custom user model to be defined in settings.py, and says we should use get_user_model() to reference the current (swapped) User model, in the docs.

Forthermore, the docs are kind to say:

If you are writing an application that interacts with the User model, you must take some precautions ...

To ensure that your test suite will pass in any project configuration, django.contrib.auth.tests.utils defines a @skipIfCustomUser decorator. This decorator will cause a test case to be skipped ...

To me it seems reasonable to expect more and more Django projects have a custom user model, and so I'd like to be able to test my app agains these projects too.

But how can I do that?

For reference, here is where it all fails:

def setUp(self):
    from django.contrib.auth import get_user_model
    User = get_user_model()
    user = User.objects.create_user(
           'test_username', 
           'test_username@example.com', 
           'da_password')  # BAM!

That last line will fail if user model does not define a username field. That is the case for CustomUser example from Django's own tests.

Community
  • 1
  • 1
frnhr
  • 12,354
  • 9
  • 63
  • 90

0 Answers0