Skip to content


Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.


If you use factoryboy you'd probably have it. If you don't use

pip install faker

Or add it to the project requirements.txt.


Faker includes a faker fixture for pytest.

def test_faker(faker):
    assert isinstance(, str)

By default it's populated with a seed of 0, to set a random seed add the following to your test configuration.


from random import SystemRandom

@pytest.fixture(scope="session", autouse=True)
def faker_seed() -> int:
    """Create a random seed for the Faker library."""
    return SystemRandom().randint(0, 999999)

Generate fake number


If you want to specify max and min values use:

faker.pyint(min_value=0, max_value=99)

Generate a fake dictionary

fake.pydict(nb_elements=5, variable_nb_elements=True, *value_types)

Where *value_types can be 'str', 'list'

Generate a fake date


Generate a random string


Create a random string with a defined format


Create an IP address


If you want a CIDR, use network=True.

Create a random choice from an Enum

pydantic uses Enum objects to define the choices of fields, so we need them to create the factories of those objects.

Sadly, there is no official provider for Enums, but NazarioJL made a custom provider.


pip install faker-enum


from enum import Enum

from faker import Faker
from faker_enum import EnumProvider

fake = Faker()

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# One of [Color.RED, Color.GREEN, Color.BLUE]

If you're using factoryboy, check this instructions.

Create Optional data


pip install fake-optional


from faker import Faker
from faker_optional import OptionalProvider

fake = Faker()

# None

# 1234

OptionalProvider uses existent faker providers to create the data, so you can use the provider method arguments.

For example, optional_int uses the python provider pyint, so you can use the min_value, max_value, and step arguments. Every optional_ method accepts the float ratio argument between 0 and 1, with a default value of 0.5 to define what percent of results should be None, a greater value will mean that less results will be None.

Check the supported methods.

Generate your own custom provider

Providers are just classes which define the methods we call on Faker objects to generate fake data.

To define a provider, you need to create a class that inherits from the BaseProvider. That class can then define as many methods as you want.

Once your provider is ready, add it to your Faker instance.

import random

from faker import Faker
from faker.providers import BaseProvider

fake = Faker()

# Our custom provider inherits from the BaseProvider
class TravelProvider(BaseProvider):
    def destination(self):
        destinations = ['NY', 'CO', 'CA', 'TX', 'RI']

        # We select a random destination from the list and return it
        return random.choice(destinations)

# Add the TravelProvider to our faker object

# We can now use the destination method:

If you want to give arguments when calling the provider, add them to the provider method.


Last update: 2021-11-27