Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Module Datafactory

Module Datafactory Laravel Package

codeception/module-datafactory

Codeception DataFactory module for generating realistic test data and fixtures in your suites. Helps create models/entities with sensible defaults and overrides, making tests cleaner, faster to write, and easier to maintain across unit, functional, and acceptance tests.

View on GitHub
Deep Wiki
Context7

codeception/module-datafactory is a Codeception module that streamlines creating and persisting test data through a simple DataFactory API. It helps you define reusable factories and generate consistent fixtures for functional/acceptance tests without repetitive setup code.

Use it to keep suites readable and maintainable by centralizing entity/record creation logic.

  • Define factories for common models/entities with sensible defaults
  • Quickly create and make records for tests (persisted or in-memory)
  • Override attributes per test to cover edge cases with minimal boilerplate
  • Integrates cleanly with Codeception workflows and helpers
  • Encourages reusable, consistent test fixtures across suites
Frequently asked questions about Module Datafactory
How do I use codeception/module-datafactory to seed test databases in a Laravel project?
First, install the module via Composer: `composer require codeception/module-datafactory --dev`. Then configure it in `codeception.yml` under `modules: enabled`. Define your factories in YAML or PHP arrays (e.g., `tests/_data/factories.yml`) and use them in tests with `$this->haveInDatabase('ModelName')`. For Laravel Eloquent models, manually map attributes to ensure compatibility.
Can I use this package for unit testing Laravel models instead of Laravel’s built-in Factory class?
This module is optimized for Codeception’s acceptance/functional tests, not unit tests. For Eloquent models, Laravel’s `DatabaseFactory` or `ModelFactory` (e.g., `create()`, `raw()`) are more integrated and efficient. Use this module for non-Eloquent data like API payloads or external service mocks where Codeception excels.
What Laravel versions does codeception/module-datafactory support, and are there any breaking changes?
The module works with Laravel 8+ and Codeception 4.5+. Laravel 9+ uses Faker 2.x, but this module defaults to Faker 1.9+. Pin Faker to `^1.9` in `composer.json` to avoid compatibility issues. Check the [Codeception docs](https://codeception.com/docs/modules/DataFactory) for updates on newer Laravel versions.
How do I ensure reproducible test data with DataFactory to avoid flaky tests?
Seed Faker with a fixed value (e.g., `$faker->seed(1234)`) in your test setup to generate deterministic data. Alternatively, define static factory configurations in YAML/JSON instead of relying on dynamic Faker methods. For Laravel, combine this with `DatabaseTransactions` in `codeception.yml` to reset the database between tests.
Does this package work with Laravel’s Eloquent models, or do I need to write custom factories?
The module doesn’t natively support Eloquent’s `Factory` class, so you’ll need to manually define factory configurations for Eloquent models in YAML/JSON or PHP. For example, map `name` and `email` fields to Faker methods, then use `$this->haveInDatabase('User')` in tests. This requires extra setup but works for basic models.
What are the performance implications of using DataFactory for large test datasets?
Dynamic data generation with Faker adds overhead, especially for large datasets. Pre-generate static fixtures in YAML/JSON or use Laravel’s `DatabaseFactory` for Eloquent models to improve speed. For API/acceptance tests, cache frequently used factory data or limit the scope of dynamic generation.
How do I configure codeception/module-datafactory for API testing in Laravel?
Install the module, then configure it in `codeception.yml` under `modules: enabled`. Define API-specific factories (e.g., `User`, `Product`) in `tests/_data/factories.yml` using Faker methods. In tests, use `$this->haveInDatabase()` or `$this->haveInMemory()` to populate data, then assert API responses with `$this->seeResponseIs()` or similar Codeception helpers.
Is there a way to integrate this with Laravel’s testing helpers like `refreshDatabase()`?
No direct integration exists, but you can combine this module with Laravel’s testing helpers. Use `DatabaseTransactions` in `codeception.yml` to reset the database between tests, and define factories in YAML/JSON for static data. For Eloquent models, consider using Laravel’s `DatabaseFactory` alongside this module for hybrid test data generation.
What alternatives exist if I don’t want to use Codeception’s DataFactory module?
For Eloquent models, Laravel’s `DatabaseFactory` (e.g., `create()`, `raw()`) is the most seamless option. For standalone Faker usage, install `fzaninotto/Faker` directly and manually generate data in tests. If you need Codeception-specific features, consider `codeception/codeception` with custom factories or third-party modules like `codeception/module-laravel5`.
How do I handle security risks with this package, given it hasn’t been updated in years?
Pin transitive dependencies like Faker to specific versions in `composer.json` to mitigate risks. Monitor the [GitHub repo](https://github.com/Codeception/module-datafactory) for forks or updates, or fork the package yourself to apply security patches. Avoid using it in production-critical projects without a maintenance plan, as stale dependencies may introduce vulnerabilities.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport