A Django plugin for pytest that provides Django-specific testing fixtures, marks, and assertions.
—
User creation and authentication fixtures for testing Django user functionality. These fixtures provide admin users, user model access, and authenticated clients for comprehensive user testing.
Create an admin user for testing administrative functionality.
def admin_user(db: None, django_user_model, django_username_field):
"""
Create an admin/superuser for testing.
Creates a superuser with username 'admin', email 'admin@example.com',
and password 'password'. The user has is_staff=True and is_superuser=True.
Args:
db: Database fixture dependency
django_user_model: Django user model class
django_username_field: Username field name
Returns:
User: Django user model instance with admin privileges
"""Usage example:
def test_admin_functionality(admin_user):
assert admin_user.is_superuser
assert admin_user.is_staff
assert admin_user.username == "admin"
assert admin_user.email == "admin@example.com"Authenticated test client logged in as admin user.
def admin_client(admin_user) -> django.test.Client:
"""
Django test client authenticated as admin user.
Returns a test client that is already logged in as the admin user,
allowing testing of admin-only views and functionality without
manual authentication.
Args:
admin_user: Admin user fixture dependency
Returns:
django.test.Client: Test client authenticated as admin
"""Usage example:
def test_admin_view_access(admin_client):
response = admin_client.get("/admin/")
assert response.status_code == 200
response = admin_client.get("/admin/myapp/mymodel/")
assert response.status_code == 200Access to the configured Django user model class.
def django_user_model(db: None):
"""
Get the configured Django user model class.
Returns the user model class as configured in Django settings
(AUTH_USER_MODEL). Useful for creating users or accessing
user model methods in tests.
Args:
db: Database fixture dependency
Returns:
Type[AbstractUser]: Django user model class
"""Usage example:
def test_user_creation(django_user_model):
User = django_user_model
user = User.objects.create_user(
username="testuser",
email="test@example.com",
password="testpass123"
)
assert user.username == "testuser"
assert user.is_activeGet the username field name for the configured user model.
def django_username_field(django_user_model) -> str:
"""
Get the username field name for the Django user model.
Returns the field name used as the username field in the
configured Django user model (usually 'username' but can
be customized in custom user models).
Args:
django_user_model: Django user model fixture dependency
Returns:
str: Username field name (e.g., 'username', 'email')
"""Usage example:
def test_username_field(django_user_model, django_username_field):
User = django_user_model
username_field = django_username_field
user_data = {username_field: "testuser", "password": "testpass123"}
user = User.objects.create_user(**user_data)
assert getattr(user, username_field) == "testuser"from django.contrib.auth.models import AbstractUser, AbstractBaseUser
from django.contrib.auth import get_user_model
from typing import Type, Any
# User model types
UserModel = Type[AbstractUser]
UsernameField = str
# User instance type (actual user object)
User = AbstractUser
# Common user fields
class UserFields:
username: str
email: str
first_name: str
last_name: str
is_staff: bool
is_active: bool
is_superuser: bool
date_joined: datetime
last_login: datetime
# User manager methods available on user model
class UserManager:
def create_user(self, username: str, email: str = None, password: str = None, **extra_fields) -> AbstractUser: ...
def create_superuser(self, username: str, email: str = None, password: str = None, **extra_fields) -> AbstractUser: ...
def get_by_natural_key(self, username: str) -> AbstractUser: ...Install with Tessl CLI
npx tessl i tessl/pypi-pytest-django