pestphp/pest-plugin-faker
Pest Plugin Faker adds seamless Faker support to Pest tests, letting you quickly generate realistic fake data in your test suite. Ideal for speeding up data-driven tests with minimal setup.
Install the package via Composer:
composer require --dev pestphp/pest-plugin-faker
No manual setup is required—once installed, Faker helpers like fake() and Faker::name() become available in your Pest tests. Start by using it in a basic test:
it('generates a random name', function () {
expect(fake()->name())->toBeString();
});
Check the official docs and Faker docs to understand available generators (name(), email(), uuid(), etc.). Your first use case will likely be seeding fake data into database factory-like test scenarios.
fake() anywhere in your test to access the Faker instance:
it('creates a user with fake data', function () {
$user = User::create([
'name' => fake()->name(),
'email' => fake()->unique()->email(),
]);
expect($user->name)->not->toBeEmpty();
});
Pest.php:
function fakeUser(array $overrides = [])
{
return array_merge([
'name' => fake()->name(),
'email' => fake()->unique()->email(),
'password' => bcrypt('password'),
], $overrides);
}
it('validates user registration', function () {
$response = $this->post('/register', fakeUser());
$response->assertRedirect();
});
User::factory()
->count(5)
->state(fn () => ['name' => fake()->name()])
->create();
fake() uses the default locale (usually en_US). Override with fake('fr_FR') or register custom Faker providers in Pest.php:
use Faker\Generator;
use Illuminate\Foundation\Testing\TestCase;
TestCase::macro('faker', fn ($locale = 'en_US') => app(Generator::class)->locale($locale));
fake()->unique()->email() may throw exceptions if uniqueness cannot be enforced (e.g., over 10,000 attempts). Guard against this in loops or use fake()->optional() for non-critical fields.fake() calls are not used outside tests/.fake() isn’t working, ensure no helper name collisions—fall back to Faker::create()->name() explicitly if needed.faker()->seed(1234)) to avoid flaky assertions.How can I help you explore Laravel packages today?