How to create admin user in django tests.py

I’d use the built-in create_superuser and log the user in before making any requests. The following should work:

from django.contrib.auth.models import User
from django.test.client import Client

# store the password to login later
password = 'mypassword' 

my_admin = User.objects.create_superuser('myuser', 'myemail@test.com', password)

c = Client()

# You'll need to log him in before you can send requests through the client
c.login(username=my_admin.username, password=password)

# tests go here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)