🎭 AutoFaker

A Python library designed to minimize the setup/arrange phase of your unit tests by removing the need to manually write code to create anonymous variables.

Build Linux Build macOS Build Windows PyPI Downloads
test_calculator.py
import unittest
from autofaker import autodata

class Calculator:
    def add(self, number1: int, number2: int):
        return number1 + number2

class CalculatorTests(unittest.TestCase):
    @autodata()
    def test_can_add_two_numbers(self, 
                                sut: Calculator, 
                                number1: int, 
                                number2: int):
        result = sut.add(number1, number2)
        self.assertEqual(number1 + number2, result)

Features

πŸ—οΈ

Built-in Types

Generate anonymous data for int, float, str, datetime, and more built-in Python types.

🎯

Class Support

Create instances of simple classes, dataclasses, nested classes, and enum classes automatically.

🎭

Fake Data

Generate realistic fake data using Faker library for names, addresses, emails, and more.

πŸ“Š

DataFrame Support

Create Pandas DataFrames with anonymous or fake data for data science testing.

πŸ”—

Nested Objects

Handle complex nested structures and circular references automatically.

πŸ“

Decorators

Use @autodata and @fakedata decorators for clean, annotation-based test setup.

Installation

πŸ”§ Development

pip install -i https://test.pypi.org/simple/ autofaker

Install the latest development version directly from GitHub.

Quick Start

Basic Usage

AutoFaker provides two main ways to generate test data:

  • Anonymous data: Random values that satisfy type requirements
  • Fake data: Realistic data using the Faker library

You can use the Autodata class directly or leverage the @autodata decorator for cleaner test code.

quick_start.py
from autofaker import Autodata

# Create anonymous built-in types
name = Autodata.create(str)
age = Autodata.create(int)
height = Autodata.create(float)

print(f"Name: {name}")
print(f"Age: {age}")
print(f"Height: {height}")

# Create multiple instances
numbers = Autodata.create_many(int, 5)
print(f"Numbers: {numbers}")

Examples

Built-in Types

from autofaker import Autodata
from datetime import datetime

# Create anonymous built-in types
text = Autodata.create(str)
number = Autodata.create(int)
decimal = Autodata.create(float)
date = Autodata.create(datetime)

print(f'String: {text}')
print(f'Integer: {number}')
print(f'Float: {decimal}')
print(f'DateTime: {date}')

DataClasses

from dataclasses import dataclass
from autofaker import Autodata

@dataclass
class Person:
    id: int
    name: str
    age: int
    email: str

# Create an anonymous Person
person = Autodata.create(Person)
print(f'ID: {person.id}')
print(f'Name: {person.name}')
print(f'Age: {person.age}')
print(f'Email: {person.email}')

Fake Data

from dataclasses import dataclass
from autofaker import Autodata

@dataclass
class Employee:
    id: int
    first_name: str
    last_name: str
    email: str
    job: str
    company: str

# Create with realistic fake data
employee = Autodata.create(Employee, use_fake_data=True)
print(f'Name: {employee.first_name} {employee.last_name}')
print(f'Job: {employee.job}')
print(f'Email: {employee.email}')

Pandas DataFrames

from autofaker import Autodata

class SalesData:
    id = -1
    product = ''
    quantity = 0
    price = 0.0

# Create DataFrame with anonymous data
df = Autodata.create_pandas_dataframe(SalesData, rows=5)
print(df)

# Create DataFrame with fake data
df_fake = Autodata.create_pandas_dataframe(SalesData, rows=5, use_fake_data=True)
print(df_fake)

Supported Data Types

Built-in Types

  • int
  • float
  • str
  • complex
  • range
  • bytes
  • bytearray

Datetime Types

  • datetime
  • date

Classes

  • Simple classes
  • @dataclass
  • Nested classes
  • Classes with lists
  • Enum classes
  • typing.Literal

DataFrames

  • Pandas DataFrame