Using Faker to Generate Filler Data for Automated Testing

Final product image
What You'll Be Creating

Faker is an open-source library created by Francois Zaninotto that generates artificial filler data for your application and its testing needs.

Faker can be used in a vanilla PHP application, a framework such as Yii or Laravel, or within a testing library such as we alluded to with Codeception in this earlier Envato Tuts+ tutorial.

In today's tutorial, I'll review the basic installation and usage of Faker and its capabilities. As Zaninotto says:

"Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you."

And, overall, it delivers a wide range of simple capabilities useful for any testing regimen.

A little reminder before we get started, I do participate in the comment threads below. I'm especially interested if you have additional thoughts or want to suggest topics for future tutorials. If you have a question or topic suggestion, please post below. You can also reach me on Twitter @reifman directly.

Getting Started

Installing Faker

I began by creating a new code tree and adding fzaninotto/faker to composer.json below:

Then, I updated composer:

Faker is installed into the vendor directory. So, then I loaded at the top of an index.php file:

Next, I wanted to try a few simple example scenarios of generating data.

Simple Examples

I extended the Faker examples as follows and ran them from http://localhost:8888/faker:

With quick refreshes, I was presented with varying results such as:

And:

And:

Faker delivers on its promise.

Faker's Default Providers

All of the data generation methods in Faker are created from the implementation of providers. Here's the code that registers Faker's default providers—it's done for you:

So above, when I requested an address from Faker, it searched all the providers for methods which matched, ultimately using the Address() provider.

You can also write your own providers or browse a number of extensions to Faker that are available on the web.

Modifiers

Faker also offers special modifiers to aid your testing, such as unique(), optional(), or valid(). For example, you can generate unique numbers:

Here's the output of unique values:

If you use the optional() method, some numbers will be returned as NULL as if the user didn't enter a field on your form. Note: I couldn't get this method to work properly.

With valid(), you can register functions which determine whether the filler data meets specific requirements or would return an error or generate an error message on a user form.

Exploring the Providers

Faker offers a broad set of methods for generating random data for your application:

  • Base: simple methods for random letters, numbers, processed strings and regex
  • Lorem Ipsum Text: random Latin text
  • Person: names of people
  • Address: mailing addresses
  • Phone Number: phone numbers
  • Company: names of companies
  • Real Text: actual text written by human beings vs. meaningless Latin strings
  • Date and Time: random dates and times
  • Internet: emails, domains, etc.
  • User Agent: browser strings
  • Payment: credit card and SWIFT strings and numbers
  • Color: random colors
  • File: file extensions, file types, and file names
  • Image: URLs of filler images of different kinds
  • Uuid: unique IDs
  • Barcode: various barcode types, e.g. ISBN13
  • Miscellaneous: encryption codes, country codes, etc.
  • Biased: random numbers with bias

Let's experiment with a few more of these methods.

Payment Information

The code below generates ten people, their credit card information and security codes:

Here's some output from the above code:

Using Faker Generating Payment or Credit Card details

Images

Using Faker Images

Here's a simple example of image generation:

But you can also generate cats:

Using Faker Images of Cats

It may be the cat generation capability that won me over. I can't wait for three-dimensional printing and soul activation to work with stuff like this.

Internationalized Data

With the code below, I created a table with four columns of names from France, Russia, America, and China:

Here's the output:

Using Faker International Capability - Tables of names from four countries

Creating Fake Email Addresses

Here's sample code to generate 25 fake email addresses from free providers such as Gmail and Yahoo:

Here's the resulting output:

Using Faker - 25 Free Email Addresses

Generating XML Documents

Faker offers a helpful example of generating XML; however, it requires that you're working with a framework and have a view architecture:

In Conclusion

I hope that this has served as a basic introduction for you to Faker, an incredibly useful free, open-source PHP library.

If you want to read further, I recommend Jim Nielsen's Filler Content: Tools, Tips and a Dynamic Example (Envato Tuts+), which provides an application designer's take on generating data. He suggests that you can be more effective when you apply fake data to create a more realistic experience during your design process.

If you'd like to know when my next Envato Tuts+ tutorial arrives, follow me @reifman on Twitter or check my instructor page. Currently, I'm working on two series you may appreciate:

  1. Programming With Yii2 series
  2. Building Your Startup with PHP about Meeting Planner; go schedule your first meeting there today.

Related Links

Tags:

Comments

Related Articles