DEV.co
Open-Source Testing · beam-community

ex_machina

ExMachina is a test data factory library for Elixir that simplifies creation of realistic test records and their associations. It works seamlessly with Ecto but is flexible enough to work with any persistence layer.

Source: GitHub — github.com/beam-community/ex_machina
2.1k
GitHub stars
143
Forks
Elixir
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorybeam-community/ex_machina
Ownerbeam-community
Primary languageElixir
LicenseMIT — OSI-approved
Stars2.1k
Forks143
Open issues3
Latest releasev2.8.0 (2024-06-25)
Last updated2026-07-07
Sourcehttps://github.com/beam-community/ex_machina

What ex_machina is

ExMachina provides a declarative DSL for defining factories that generate test data with support for sequences, lazy evaluation, nested associations, and both map/struct and non-map factories. It includes ExMachina.Ecto for direct database insertion and standalone mode for pure data generation.

Quickstart

Get the ex_machina source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/beam-community/ex_machina.gitcd ex_machina# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Ecto-based Phoenix or Elixir web applications

Primary use case: generating realistic test data and persisted fixtures for controller, model, and integration tests with automatic association insertion.

Complex domain model testing

Building deeply nested data structures with multiple associations, sequences, and conditional attributes without manual setup boilerplate in test cases.

Parameterized test generation

Creating variations of test data (build_list, build_pair, params_for) to reduce test case duplication and improve readability.

Implementation considerations

  • Factories must be defined in test/support (non-Phoenix) or lib (if compiled in test env); requires elixirc_paths configuration in mix.exs for non-Phoenix projects.
  • Application.ensure_all_started(:ex_machina) must be called in test_helper.exs before ExUnit.start to enable factory compilation.
  • Sequences are module-level state; care needed in parallel test suites to avoid collisions; consider using sequence(:name, &fn/1) with consistent logic.
  • Lazy attribute evaluation via anonymous functions delays execution; useful for fresh instances per record but adds complexity if overused.
  • Default factory behavior merges attributes automatically; custom factories with arity/1 require manual merge_attributes/2 and evaluate_lazy_attributes/1.

When to avoid it — and what to weigh

  • Non-Elixir/non-Ecto projects — Language-specific to Elixir. Not applicable to Python, Node.js, Ruby, or other ecosystems (use FactoryBot, factory-boy, etc. instead).
  • Real production data generation — ExMachina is test-only; it is not designed for seeding production databases or generating production fixtures.
  • Non-struct/non-map data models — While non-map factories exist, Ecto integration requires Ecto schemas. Custom persistence requires significant manual configuration.
  • Minimal test overhead projects — Small scripts or projects with no associations or complex data may see dependency overhead; inline test data may be simpler.

License & commercial use

MIT License. Permissive open-source license permitting commercial use, modification, and distribution with attribution and liability disclaimer.

MIT is a permissive OSI-approved license. Commercial use in private applications is permitted. Redistribution or bundling requires license file inclusion and attribution. Verify with legal if redistribution or SaaS license terms are material to your use case.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

ExMachina is test-only and not exposed to production. No direct security risk. Ensure factories do not hardcode sensitive test credentials or expose production data patterns; use sequences or Faker library for realistic but safe dummy values.

Alternatives to consider

Ecto.Factory or custom fixtures

Lower-level; requires manual association setup and sequence management. Simpler for minimal factories but scales poorly.

Faker (Elixir library)

Generates random fake data (names, emails, etc.) but does not create persistent records or manage associations. Often used alongside ExMachina.

Manual map/struct creation in tests

Direct control and no dependencies but leads to repeated boilerplate, brittle tests, and tight coupling to schema changes.

Software development agency

Build on ex_machina with DEV.co software developers

ExMachina eliminates boilerplate in test fixtures. Explore the docs, review the README examples, and evaluate integration with your Ecto or custom repository.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

ex_machina FAQ

Can ExMachina be used without Ecto?
Yes. Use `use ExMachina` (not ExMachina.Ecto) to define factories for plain maps or custom structs. No database persistence; only build/2, build_pair/2, etc. No insert/* functions.
How do I avoid creating the same associated record multiple times in one test?
Use lazy evaluation: `insert_pair(:account, user: fn -> build(:user) end)` creates a fresh user per account. Without the fn, both accounts share the same user instance.
Do I need to configure ExMachina in my Phoenix app?
No special configuration for Phoenix. Standard installation in mix.exs, test/support/factory.ex, and ensure Application.ensure_all_started(:ex_machina) in test_helper.exs.
Can I use ExMachina in non-test environments?
Not recommended; ExMachina is test-focused. For production seeding, use Ecto.Repo directly or a dedicated seeding script. ExMachina can be compiled in all environments but adds unnecessary dependency.

Software developers & web developers for hire

DEV.co is a software development agency delivering custom software development services to companies building on open source. Our software developers and web developers design, integrate, and ship production systems — spanning web development, APIs, AI, data, and cloud. If ex_machina is part of your open-source testing roadmap, our team can implement, customize, migrate, and maintain it.

Ready to streamline your Elixir test data?

ExMachina eliminates boilerplate in test fixtures. Explore the docs, review the README examples, and evaluate integration with your Ecto or custom repository.