autofaker
autofaker is a Python library designed to minimize the setup/arrange phase of unit tests by automatically generating anonymous variables for various data types and classes. It supports built-in types (int, str, float), datetime types (datetime, date), and simple, nested, or dataclasses. As of version 1.0.22, it remains actively developed with a focus on simplifying test data generation.
Common errors
-
TypeError: Parameter 'obj_type' cannot be None
cause The `Fake` function was called with `None` as the type argument, or an unresolvable type hint that defaults to `None`.fixEnsure that the argument passed to `Fake()` is a valid type (e.g., `str`, `int`, `MyClass`) and not `None`. Double-check type hints in classes. -
AttributeError: module 'autofaker' has no attribute 'Fake'
cause This usually means you're trying to access `autofaker.Fake` after `import autofaker` instead of `from autofaker import Fake`.fixChange your import statement to `from autofaker import Fake` to directly import the `Fake` function, or explicitly call `autofaker.Fake()` if you prefer the module import. -
TypeError: __init__() missing 1 required positional argument: 'field_name'
cause This error can occur when trying to `Fake()` a class that has mandatory constructor arguments without default values, and autofaker cannot infer how to create them (e.g., non-type-hinted arguments or complex custom types).fixAdd explicit type hints to all fields in your class, especially required ones, to help autofaker infer how to populate them. For example, `field_name: str`.
Warnings
- gotcha autofaker relies heavily on type hints to generate meaningful data for class attributes. If type hints are missing, especially for custom classes or complex structures, autofaker might not be able to infer the desired type or might default to `None`.
- gotcha When dealing with `default_factory` in dataclasses, be aware that it will take precedence over autofaker's generation for that specific field. If you want autofaker to handle the default value generation, ensure your `default_factory` does not conflict or explicitly uses `Fake()` within it.
- gotcha While autofaker supports nested classes, excessively deep or mutually recursive class definitions can lead to `RecursionError`. The library does not implement infinite recursion protection for all complex scenarios.
Install
-
pip install autofaker
Imports
- Fake
import autofaker; autofaker.Fake()
from autofaker import Fake
Quickstart
from autofaker import Fake
from dataclasses import dataclass
import datetime
@dataclass
class Product:
name: str
price: float
is_available: bool
@dataclass
class Customer:
customer_id: str
name: str
email: str
age: int
registration_date: datetime.date
products_bought: list[Product]
# Generate a fake instance of the Customer class
fake_customer = Fake(Customer)
print(f"Fake Customer Name: {fake_customer.name}")
print(f"Fake Customer Email: {fake_customer.email}")
# Generate a fake string
fake_string = Fake(str)
print(f"Fake String: {fake_string}")
# Generate a fake integer
fake_int = Fake(int)
print(f"Fake Integer: {fake_int}")