pythondjangofactoryfakerfactory-boy

How to use Faker from Factory_boy


Factory_boy uses fake-factory (Faker) to generate random values, I would like to generate some random values in my Django tests using Faker directly.

Factory_boy docs suggests using factory.Faker and its provider as :

class RandomUserFactory(factory.Factory):
    class Meta:
        model = models.User

    first_name = factory.Faker('first_name')

But this isn't generating any name:

>>> import factory
>>> factory.Faker('name')
<factory.faker.Faker object at 0x7f1807bf5278>
>>> type(factory.Faker('name'))
<class 'factory.faker.Faker'>

From factory_boy faker.py class factory.Faker('ean', length=10) calls faker.Faker.ean(length=10) but Faker docs says it should show a name:

from faker import Faker
fake = Faker()
fake.name()
# 'Lucy Cechtelar'

Is there any other way to use Faker instead of setting an instance directly from Faker?

from faker import Factory
fake = Factory.create()
fake.name()

Solution

  • I know this is an old question but for anyone who might come across this, here's another approach that you can use.

    >>> from factory.faker import faker
    >>> FAKE = faker.Faker()
    >>> FAKE.name()
    'Scott Rodriguez'
    >>> FAKE.address()
    'PSC 5061, Box 1673\nAPO AP 53007'
    >>>