2

I have a Django application and the custom user - want to test it (do unit testing). My custom user is emailuser, which consists of e-mail and password fields. I want to set up something of such national mplayer, give it a field and a password. But my code below does not work.

settings.py

AUTH_USER_MODEL = 'custom_user.EmailUser'

My code in test.py

from django.test import TestCase
from django.conf import settings

class MyTest(TestCase):
    def setUp(self):
        self.user = settings.AUTH_USER_MODEL.objects.create('[email protected]', 'testpass')

    def test_user(self):
        self.assertEqual(self.user, '[email protected]')

2 Answers 2

6

Well, try this:

from django.test import TestCase
from django.contrib.auth import get_user_model

User = get_user_model()

class CustomUserTestCase(TestCase):

    def test_create_user(self):
        # params = depends on your basemanager’s create_user methods.
        user = User.objects.create(**params)
        self.assertEqual(user.pk, user.id)
Sign up to request clarification or add additional context in comments.

Comments

2

You should use the create_user() method instead of just create(). Also you should check the email field, not the user itself.

class MyTest(TestCase):
    def setUp(self):
        self.user = settings.AUTH_USER_MODEL.objects.create_user(
                                              '[email protected]', 'testpass')

    def test_user(self):
        self.assertEqual(self.user.email, '[email protected]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.