QuickFactory

A lightweight, flexible factory library for Elixir that makes test data generation simple and intuitive.

Features

Quick Start

1. Define a Factory

defmodule MyApp.UserFactory do
use QuickFactory,
schema: MyApp.User,
repo: MyApp.Repo,
changeset: :changeset
def build(params \\ %{}) do
%{
name: "John Doe",
email: "john@example.com",
age: 25,
first_name: "John",
last_name: "Doe"
}
|> Map.merge(params)
end
end

2. Generate Test Data

# Build a struct
user = QuickFactory.build(UserFactory)
#=> %User{name: "John Doe", email: "john@example.com", age: 25}
# Build with custom params
user = QuickFactory.build(UserFactory, %{name: "Jane"})
#=> %User{name: "Jane", email: "john@example.com", age: 25}
# Generate params only (great for API testing)
params = QuickFactory.build_params(UserFactory, %{age: 30})
#=> %{name: "John Doe", email: "john@example.com", age: 30}
# Insert to database
user = QuickFactory.insert!(UserFactory)
# Generate multiple records
users = QuickFactory.insert_many!(3, UserFactory)

3. Key Transformations

Perfect for API testing with different key formats:

# Default: atom keys
QuickFactory.build_params(UserFactory)
#=> %{name: "John Doe", first_name: "John", last_name: "Doe"}
# String keys
QuickFactory.build_params(UserFactory, %{}, keys: :string)
#=> %{"name" => "John Doe", "first_name" => "John", "last_name" => "Doe"}
# CamelCase string keys (great for JSON APIs)
QuickFactory.build_params(UserFactory, %{}, keys: :camel_string)
#=> %{"name" => "John Doe", "firstName" => "John", "lastName" => "Doe"}

Installation

Add quick_factory to your list of dependencies in mix.exs:

def deps do
[
{:quick_factory, "~> 0.1.0", only: :test}
]
end

Why QuickFactory?

Documentation

For full documentation, visit HexDocs.

Credits

Most of the code was taken from