pytest-factoryboy
pytest-factoryboy copied to clipboard
What sorcery is this?
I am trying to make some tests and this is my first time using pytest-factoryboy. I would like to understand why my does my print() doesn't print the same for both objects. I thought they were from the same class (UserFactory).
conftest.py
from pytest_factoryboy import register
from bin.tests.factories import UserFactory
register(UserFactory)
factories.py
import factory
from pytest_factoryboy import register
from faker import Factory as FakerFactory
import datetime
import pytest
from bin import models
fake = FakerFactory.create()
@register
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.User
django_get_or_create = ('username',)
username = factory.LazyAttribute(lambda n: fake.user_name())
email = factory.LazyAttribute(lambda n: fake.safe_email())
first_name = factory.LazyAttribute(lambda n: fake.first_name_female())
last_name = factory.LazyAttribute(lambda n: fake.last_name_female())
def __init__(self, *args, **kwargs):
super(UserFactory, self).__init__(*args, **kwargs)
self.set_password('1234')
@classmethod
def _create(cls, model_class, *args, **kwargs):
"""Override the default ``_create`` with our custom call."""
manager = cls._get_manager(model_class)
# The default would use ``manager.create(*args, **kwargs)``
user = manager.create_user(*args, **kwargs)
user.is_active = True
user.save()
return user
test_user.py
import pytest
from django.core.urlresolvers import reverse
from django.conf import settings
class TestUser:
@pytest.mark.integration
def test_user_gets_locked_after_unsuccessful_attempts(self, db, user_factory, client):
client.get(reverse('logout'))
for _ in range(0, 10):
response = client.post(
reverse('login'),
{
'username': user_factory.username,
'password': 'some_wrong_password'
}
)
print(user_factory.username)
another_user = UserFactory()
print(another_user.username)
assert user_factory.profile.pwd_attempts > 9
assert not user_factory.is_active
Ouput
bin/tests/test_user.py:27: AttributeError
----------------------------- Captured stdout call -----------------------------
<UserFactory for <class 'django.contrib.auth.models.User'>>
tinamendoza
Is it because the fixture name is created after the models class and not the Factory-class name?