Using faker to generate fake data

Use pip to install faker:

$ pip install faker
..................

Examples:

>>> from faker import Faker
>>> fake = Faker()
>>> fake.name()
'Lauren Mendez'
>>> fake.phone_number()
'4390337172'
>>> fake.email()
'melissa99@example.net'
>>> fake.city()
'Port Benjaminmouth'

Faker uses providers to generate fake data. Each provider adds methods to the Faker instance. E.g. name() method is implemented by faker.providers.person while address() method is implemented by the faker.providers.address provider. Check the list of standard providers and the community providers to find out what methods can be used.

Some of providers are also localized providers:

>>> fake_bg = Faker("bg-BG")
>>> fake_bg.name()
'Др. Войнка Яркова'

Faker pytest fixture

Faker has its own pytest plugin which provides a faker fixture which can be used directly by tests.

def test_person_is_saved(faker):
   name = faker.name()
   address = faker.address()
   _ = Person(name, address)
   person = Person.objects.get(name=name)
   assert address == person.address

Faker also provides customization for the faker fixture through autouse fixtures:

import pytest

@pytest.fixture(scope='session', autouse=True)
def faker_session_locale():
   return ['it_IT', 'ja_JP', 'en_US']

@pytest.fixture(scope='session', autouse=True)
def faker_seed():
   return 12345

For further details check the faker pytest fixtures documentation.

Further reading