DEV.co
Open-Source Testing · thoughtbot

factory_bot

factory_bot is a Ruby test data library that replaces fixtures with a flexible factory pattern, allowing developers to define and generate test objects easily. It supports multiple build strategies and works seamlessly with Rails applications through the factory_bot_rails gem.

Source: GitHub — github.com/thoughtbot/factory_bot
8.2k
GitHub stars
2.6k
Forks
Ruby
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
Repositorythoughtbot/factory_bot
Ownerthoughtbot
Primary languageRuby
LicenseMIT — OSI-approved
Stars8.2k
Forks2.6k
Open issues71
Latest releasev6.6.0 (2026-05-04)
Last updated2026-06-22
Sourcehttps://github.com/thoughtbot/factory_bot

What factory_bot is

A mature Ruby gem providing factories (object templates) with multiple build strategies (save to DB, in-memory, attributes-only, stubbed), trait composition, and inheritance. Integrates with Rails ORM and test frameworks via adapters documented in the wiki.

Quickstart

Get the factory_bot source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/thoughtbot/factory_bot.gitcd factory_bot# follow the project's README for install & configuration

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

Best use cases

Rails Test Suite Setup

Generating consistent, readable test data in Rails unit and integration tests. Reduces boilerplate compared to fixtures and enables dynamic, conditional object creation per test.

Complex Object Hierarchies

Building related objects (User with multiple Accounts, Posts with Comments) through associations and traits. Inheritance of factory definitions for variants (admin_user vs regular_user).

RSpec and Test Framework Integration

Out-of-the-box compatibility with RSpec, Minitest, and other Ruby test runners. Provides clean DSL for inline factory definitions or separate factory files.

Implementation considerations

  • Install via `bundle add factory_bot` or `gem install factory_bot`; for Rails projects, also add factory_bot_rails gem for Rails-specific helpers.
  • Check supported Ruby versions in `.github/workflows/build.yml` to ensure compatibility with your Ruby runtime.
  • Define factories in `spec/factories` or `test/factories` directory; use traits and inheritance to reduce duplication and keep definitions DRY.
  • Choose appropriate build strategy per test (create, build, attributes_for, build_stubbed) to balance realism and speed.
  • Monitor factory usage patterns with FactoryTrace tool to identify and remove unused factories as codebase evolves.

When to avoid it — and what to weigh

  • Non-Rails Ruby Projects Without Adapter — factory_bot requires explicit setup outside Rails. If using Sequel, DataMapper, or non-ORM code, integration effort increases and community support is limited.
  • Performance-Critical Test Suites at Scale — Extensive use of factories with deep associations can slow test runs due to repeated object creation and validation. May need careful profiling and optimization with FactoryTrace.
  • Team Unfamiliar with Ruby/Rails Conventions — Requires understanding of Ruby DSL, Rails associations, and test-driven development patterns. Learning curve steeper for teams new to Ruby or the factory pattern.
  • Strict No-Network or Offline Testing — If tests must avoid all database I/O and external calls, using `build` or `attributes_for` strategies requires discipline; default `create` strategy persists to DB.

License & commercial use

MIT License (permissive, OSI-approved). Allows use, modification, distribution, and commercial use with attribution and no warranty.

MIT license permits commercial use in proprietary products without royalties or source disclosure. No legal review needed for license compliance, but verify internal IP policies. No official SLA or commercial support tier stated in the data; commercial support may be available via thoughtbot (vendor) but requires separate inquiry.

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

No inherent security vulnerabilities disclosed in the data. Security considerations are inherited from the host Ruby/Rails environment and database. Ensure test data does not include production secrets or PII; factory definitions should use fixtures or environment-based values for sensitive data. No security audit or vulnerability history provided in the data; review the GitHub security advisory feed and dependencies for transitive risk.

Alternatives to consider

Fabrication

Lightweight DSL for Ruby object generation with similar factory pattern. Smaller community and fewer integrations; consider if you prefer a simpler, less opinionated tool.

Fixtures (Rails native)

Built-in Rails fixture files (YAML/CSV). No external dependency but less readable, harder to maintain, and inflexible compared to factories for dynamic test data.

Sequel factories or custom builders

For non-Rails projects or Sequel ORM, custom factory patterns or Sequel-native builders may reduce dependency bloat. Requires in-house support and standardization.

Software development agency

Build on factory_bot with DEV.co software developers

Evaluate factory_bot for your Rails or Ruby project. Check the factory_bot book and wiki for detailed guides, then integrate via Gemfile. Start with a single test suite and scale as needed.

Talk to DEV.co

Related open-source tools

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

Related on DEV.co

Explore the category and the services that help you build with it.

factory_bot FAQ

Does factory_bot work without Rails?
Yes, but requires manual setup of ORM adapters and configuration. Rails integration is easiest via factory_bot_rails gem. Non-Rails projects must register ActiveRecord-compatible ORMs or implement custom build/save logic.
What is the difference between build, create, and attributes_for?
build: instantiates object in memory without saving. create: saves to DB. attributes_for: returns a hash of attributes only. build_stubbed: returns an object with DB ID assigned but not persisted. Choose based on test isolation and speed needs.
How do I handle associations in factories?
Define associations using `association :user` syntax in the factory definition. factory_bot automatically creates related records. Use `build` strategy on associations to avoid DB overhead if not needed for the test.
Is factory_bot suitable for large test suites?
Yes, but requires optimization. Use appropriate build strategies, avoid deep nested associations, and profile with FactoryTrace to remove unused factories. Consider test suite parallelization and database cleanup strategies.

Software development & web development with DEV.co

From first prototype to production, DEV.co delivers software development services around tools like factory_bot. Our software development agency staffs experienced software developers and web developers for custom software development, web development, integrations, and ongoing support across open-source testing and beyond.

Ready to streamline your Ruby test suite?

Evaluate factory_bot for your Rails or Ruby project. Check the factory_bot book and wiki for detailed guides, then integrate via Gemfile. Start with a single test suite and scale as needed.