Generating Test Data with Faker and friends
Posted: January 3, 2017 Filed under: Software Development, Test Automation, Testing | Tags: Faker, Java, JavaScript, Ruby, Test Data, Test Data Management Leave a commentDuring my recent endeavours to build a REST API, I wanted to test the bulk upload feature of the API which required a large set of test data. I was looking for a quick way to create a fake test data set and found an interesting Ruby library called Faker. This a port of Perl’s Data::Faker library.
Faker is a popular choice in Rails community. It is also ported to Java, Python and JavaScript.
Faker provides a number of categories for test data generation. For example Names, Addresses, Pictures, Business & Finance (Credit Card Numbers, IBAN numbers, Swift Codes etc.), Text placeholders and much more.
Let’s say we want to create a list of records on the fly. We want a list of names and contact phone number. In Ruby, all we would have to do is:
First install Faker Gem with:
gem install faker
and create a simple script to generate records:
require 'faker' require 'json' landlords = [] 100.times do landlord = Hash.new landlord["name"] = Faker::Name.name landlord["contact_number"] = Faker::PhoneNumber.cell_phone landlords.push(landlord) end puts JSON.pretty_generate(landlords)
This will create records similar to below output. You can copy or store the output and use it seed to database or during the testing
[ { "name": "Ms. Adelia Ortiz", "contact_number": "1-443-107-3897" }, { "name": "Delores Cassin", "contact_number": "868.775.6054" }, { "name": "Rose Klocko", "contact_number": "759-090-9777" }, { "name": "Josiah Langworth I", "contact_number": "1-308-497-4606" }, { "name": "Kattie Hamill", "contact_number": "266-980-1233" }, ]
We can also use Faker in automated tests. For example here’s Capybara test using a fake user data to test Sign-Up feature:
name = Faker::Name.name email = Faker::Internet.email visit("/signup") fill_in('account_name', with: name) fill_in('account_email', with: email) click_button('Sign Up') page.has_content?("Dear ${name}") page.has_content?('Verify Your Email Address')
Here’s JavaScript version of the Ruby script made with Faker.js
var faker = require('faker') landlords = [] for(i=0; i<100; i++) { var user = { name: faker.name.findName(), email: faker.phone.phoneNumber(), }; landlords.push(user) } console.log(landlords);
By default, these libraries generate values which may not be unique. However, you can set options to generate unique values. You can also extend/customise the output.
You can find Java port of the Faker called Java Faker Here’s an example using Java Faker for Registration Test
package com.example; import com.github.javafaker.Faker; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.testng.annotations.Test; public class RegisterANewUser extends BaseTest { WebDriver driver; Faker faker; @Test public void shouldRegisterAUser() { driver = getDriver(); faker = new Faker(); String name = faker.name().fullName(); String company = faker.company().name(); String email = faker.internet().emailAddress(); driver.get("http://www.blazedemo.com/register"); driver.findElement(By.id("name")) .sendKeys(name); driver.findElement(By.id("company")) .sendKeys(company); driver.findElement(By.id("email")) .sendKeys(email); driver.findElement(By.id("password")) .sendKeys("p@assword"); driver.findElement(By.id("password-confirm")) .sendKeys("p@assword"); driver.findElement(By.className("btn")).click(); String userNameLabelText = driver.findElement(By.className("dropdown-toggle")).getText(); Assert.assertEquals(name, userNameLabelText); Assert.assertTrue(driver.getPageSource().contains("Dashboard")); Assert.assertTrue(driver.getPageSource().contains("You are logged in!")); } }
There is also Python port available at https://github.com/joke2k/faker
Faker and friends are must have power tool for Developers and testers.