import pytest
import factory
# Simulated User model for testing
class User:
def __init__(self, username, email, is_active=True):
self.username = username
self.email = email
self.is_active = is_active
# Factory class for User
class UserFactory(factory.Factory):
class Meta:
model = User
username = "testuser"
email = "testuser@example.com"
is_active = True
@pytest.fixture
def user_factory():
return UserFactory
def test_create_user_with_factory(user_factory):
user = user_factory()
assert user.username == "testuser", f"Expected username 'testuser', got {user.username}"
assert user.email == "testuser@example.com", f"Expected email 'testuser@example.com', got {user.email}"
assert user.is_active is True, f"Expected is_active True, got {user.is_active}"
This test uses factory_boy to create a UserFactory that generates User instances with default values.
The user_factory pytest fixture returns the factory class, so the test can call it to create a user.
The test test_create_user_with_factory calls the factory to create a user and then asserts the username, email, and is_active fields match the expected defaults.
Assertions include messages to help understand failures.
This approach keeps test data creation clean and reusable.