# thoughtbot/factory_bot

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/thoughtbot-factory-bot).**

8,396 stars · 2,578 forks · Ruby · mit

## Links

- GitHub: https://github.com/thoughtbot/factory_bot
- Homepage: https://thoughtbot.com
- awesome-repositories: https://awesome-repositories.com/repository/thoughtbot-factory-bot.md

## Topics

`factories` `factory-bot` `factory-girl` `fixtures` `rails` `ruby` `rubygems` `testing` `thoughtbot`

## Description

FactoryBot is a Ruby library for generating complex test objects and their associations, serving as a dynamic alternative to static fixture files. It provides a system for defining reusable data blueprints with default attributes and inheritance to produce consistent test records.

The tool distinguishes itself through flexible instantiation strategies, allowing users to control whether objects are persisted to a database, built in memory, or created as stubs. It manages data uniqueness via a sequence generator for incremental values and uses traits to bundle shared attributes into reusable modules that can be mixed into objects during creation.

The project covers broad capability areas including automated object association management, bulk record generation, and lifecycle hooks for executing custom logic during the build process. It also includes utilities for validating factory definitions and tracking execution through instrumentation.

## Tags

### Testing & Quality Assurance

- [Test Data Factories](https://awesome-repositories.com/f/testing-quality-assurance/general-testing-utilities/test-utilities-assertions/test-data-management/test-data-factories.md) — Implements a comprehensive system for defining reusable data blueprints and factories to generate consistent test environments.
- [Test Data Blueprinting](https://awesome-repositories.com/f/testing-quality-assurance/test-data-blueprinting.md) — Provides a system for creating reusable templates with default attributes and nested inheritance to reduce repetition. ([source](https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md))
- [Automated Test Data Generation](https://awesome-repositories.com/f/testing-quality-assurance/automated-test-data-generation.md) — Creates flexible and reusable object blueprints to replace static fixture files in a testing environment.
- [Database Object Mocking](https://awesome-repositories.com/f/testing-quality-assurance/database-object-mocking.md) — Produces persisted or in-memory Ruby objects with complex associations for use in automated test suites.
- [Test Instantiation Strategies](https://awesome-repositories.com/f/testing-quality-assurance/test-instantiation-strategies.md) — Provides flexible strategies to control whether test objects are persisted to a database, built in memory, or created as stubs.
- [Testing Object Emulation](https://awesome-repositories.com/f/testing-quality-assurance/testing-object-emulation.md) — Creates complex Ruby objects and their associations for testing as a dynamic alternative to static fixture files.
- [Alphanumeric Sequence Generation](https://awesome-repositories.com/f/testing-quality-assurance/alphanumeric-sequence-generation.md) — Includes a sequence generator to produce incremental or cycling values, ensuring attribute uniqueness across test records.
- [Bulk Test Record Generation](https://awesome-repositories.com/f/testing-quality-assurance/bulk-test-record-generation.md) — Produces arrays of objects in bulk using list methods while allowing attribute overrides for individual items. ([source](https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md))
- [Composite Generation Strategies](https://awesome-repositories.com/f/testing-quality-assurance/composite-generation-strategies.md) — Generates test objects using various strategies, including persisted instances and attribute hashes. ([source](https://cdn.jsdelivr.net/gh/thoughtbot/factory_bot@main/README.md))
- [Test Data Isolation](https://awesome-repositories.com/f/testing-quality-assurance/test-data-isolation.md) — Provides unique and consistent data records for tests to ensure isolation and prevent data collisions.

### Part of an Awesome List

- [Variant Definitions](https://awesome-repositories.com/f/awesome-lists/devtools/object-factories/variant-definitions.md) — Allows defining multiple factories for a single class and using inheritance to share common attributes across different roles. ([source](https://thoughtbot.github.io/factory_bot))

### Data & Databases

- [Test Attribute Grouping](https://awesome-repositories.com/f/data-databases/attribution-models/test-attribute-grouping.md) — Bundles shared attributes into reusable traits to mix them into objects during instantiation. ([source](https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md))
- [Automated Association Generation](https://awesome-repositories.com/f/data-databases/object-relational-mapping-associations/automated-association-generation.md) — Automatically resolves and creates linked test objects based on class associations.
- [Automatic Association Linking](https://awesome-repositories.com/f/data-databases/object-relational-mapping-associations/automatic-association-linking.md) — Links factories together to automatically create related objects through implicit naming and polymorphic relationships. ([source](https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md))
- [Test Object Associations](https://awesome-repositories.com/f/data-databases/object-relational-mapping-associations/test-object-associations.md) — Automatically links related Ruby objects through implicit naming and polymorphic relationships during test data generation.
- [Persistence Strategies](https://awesome-repositories.com/f/data-databases/persistence-strategies.md) — Provides flexible strategies to control whether objects are persisted to a database, built in memory, or stubbed.
- [Test Sequence Generation](https://awesome-repositories.com/f/data-databases/data-management/unique-identifier-generators/test-sequence-generation.md) — Produces incremental or cycling values for attributes to ensure data uniqueness across multiple test records. ([source](https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md))
- [Sequential Value Generation](https://awesome-repositories.com/f/data-databases/data-management/unique-identifier-generators/uniqueness-enforcement/duplicate-prevention/sequential-value-generation.md) — Maintains internal counters to provide incremental or cycling values for attributes to prevent data duplication.

### Development Tools & Productivity

- [Blueprint Inheritance](https://awesome-repositories.com/f/development-tools-productivity/environment-profiles/configuration-inheritance/blueprint-inheritance.md) — Enables factories to inherit attributes from parent definitions to create specialized object variants.
- [Dynamic Templates](https://awesome-repositories.com/f/development-tools-productivity/project-scaffolding-config-code-generation/project-scaffolding-configuration/templating-engines/dynamic-templates.md) — Provides a simple syntax for creating dynamic blueprints that replace static fixtures with generated data. ([source](https://thoughtbot.github.io/factory_bot))

### Programming Languages & Runtimes

- [Test Data Generators](https://awesome-repositories.com/f/programming-languages-runtimes/language-features-paradigms/language-features/core-conceptual-frameworks/programming-language-concepts/random-number-generation/random-number-generators/random-data-generators/test-data-generators.md) — Creates complex objects and their associations for testing to replace the need for static fixture files. ([source](https://github.com/thoughtbot/factory_bot/blob/main/NAME.md))

### Software Engineering & Architecture

- [Attribute Traits](https://awesome-repositories.com/f/software-engineering-architecture/architectural-design-patterns/object-oriented-foundations/object-oriented-programming/behavior-customization/entity-behavior-modules/trait-based-compositions/attribute-traits.md) — Bundles shared attributes into reusable traits that can be dynamically added to objects during instantiation.
- [Dynamic Object Instantiation](https://awesome-repositories.com/f/software-engineering-architecture/dynamic-object-instantiation.md) — Provides a system for creating complex test objects from reusable blueprint definitions and hierarchical configurations.
- [Lifecycle Callbacks](https://awesome-repositories.com/f/software-engineering-architecture/lifecycle-callbacks.md) — Implements build and creation callbacks to execute custom setup logic at specific stages of the object lifecycle.
