How to generate test data for a web application

I’ve coded my fair share of websites that require user authentications in some form or another. Often, you’ll need to test registration forms, user details, billing/delivery address, foreign characters and all kinds of fluff related to user accounts.

Most of the time, this will be ‘user test’, ‘test1’, ‘test2’, ‘aaa’, ‘foo’, ‘123’. Sound familiar?

Sometimes you need believable information, either for testing purposes (fuzzy unit tests anyone?) or to show as a placeholder for real data on a website design. I’ve found the Faker PHP library is perfect for generating this kind of data.

Faker can generate a wide variety of testing data for your web application. This includes:

  • names
  • addresses
  • telephone numbers
  • companies
  • email addresses
  • websites
  • lorem ipsum
  • images
  • catch phrases
  • credit/debit card details
  • date times
  • user agents
  • colours

Its very easy to use as well. At its simplest, just include the library, instantiate its main class, and access its sensibly named attributes. It returns a string result as you’d expect.

require_once '/path/to/Faker/src/autoload.php';
$faker = Faker\Factory::create();

echo $faker->name;
echo $faker->address;
echo $faker->text; // lorem ipsum text

// Generate multiple different names
for ($i=0; $i < 10; $i++)
{
    echo $faker->name, "\n";
}

I’ve found this very useful. Take a look at the Faker GitHub pages.

Leave a Reply

Your email address will not be published. Required fields are marked *