9951 explained code solutions for 126 technologies


phpunitHow to use Faker with PHPUnit?


Faker is a PHP library that generates fake data for you. It can be used with PHPUnit to generate test data for your tests.

Example code

<?php
use Faker\Factory;

$faker = Factory::create();

echo $faker->name;

Output example

John Smith

Code explanation

  • use Faker\Factory; - imports the Faker library
  • $faker = Factory::create(); - creates a new Faker instance
  • echo $faker->name; - prints a random name

Helpful links

Edit this code on GitHub