Automation Testing

Dynamic Test Automation Data Using a Faker

By Test Guild
  • Share:
Join the Guild for FREE
Faker

Have you ever had to automate an application that required unique data for every test run?

It can be a pain.

I use to have to automate a hospital application that required that I register a new patient in every test before the rest of the workflow could proceed.

The only issue was that each username and social security number had to be unique.

Hardcoded data would not work in this scenario and seeding the database with users beforehand than cleaning up after was messy and time-consuming.

What to do? Faker to the rescue.

k6 Performance Test Tool

What is a Data Faker?

A faker allows you to dynamically create real looking data like names, phone numbers, addresses, SSN — all kinds of stuff.

Lots of times you don't care about what these values like name or address are so long as you know that your web application will save them correctly.

Using a faker allows you to model this data and create fresh dynamic values for these fields ever test run.

This approach has been recommended a few times on my test automation podcast TestTalks. For example, check out my interview with Paul Merrill on Data Strategies in Testing

Also, Titus Fortner showed an example in his Automation Guild 2018 session on using a Faker in his Crafting a Test Framework session.

Selenium Data Faker Example

Say for example I’m testing an application as if I was a user in Italy. Using faker and passing it a locale of it_IT(Italian) it will generate Italian names and address. Here is an example using Selenium with Python with a Faker

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from faker import Faker

fake = Faker('it_IT')
full_name = (fake.name())

print(full_name)
print(fake.address())

first,last = full_name.split()

driver = webdriver.Chrome('C://Download/chromedriver.exe')
driver.get("https://testguild.com/SeleniumTestPage.html")
assert "WebDriver" in driver.title
first_name = driver.find_element_by_name("fname")
last_name = driver.find_element_by_name("lname")
first_name.clear()
first_name.send_keys(first)
last_name.send_keys(last)
first_name.send_keys(Keys.RETURN)

driver.close()

This simple code generates a dynamic name which I parse into first and last names. I then use those values as inputs in my test applications first and last name fields.

It’s as simple as that.

Of course, you can get as crazy as you want, but as you can see this is powerful even in a simple scenario.

Test Management Machine Learning Robot

When You Might Want to Use a Faker in Your Test Automation

As we have seen using a faker for values that need to be unique like SSN is a prime user case for utilizing a faker.

  • If you are demoing a product to a customer having realistic looking data can help improve the experience. Faker can seed your application with realistic looking data.
  • Faster and less maintenance than using hardcoded values
  • Since you are using dynamic data, it might uncover an unanticipated bug.
  • Test that requires a broad set of test data with a bulk activity.

How to Find a Faker for Your Automation Testing Language

If you're using an automation tool that allows you to import libraries, you are in luck since there are a bunch of fakers available in most languages. Don’t waste your time creating your custom code to do what an exiting open source library can already do.

There are Faker libraries in many languages, check them out: (Let me know if I missed one of your favorites and I will add it to the list)

Don't over Fake it

Like anything sometimes going overboard can cause more issue than its worth.

Use your judgment when using a Faker and don’t over complicate your test script with unnecessary dynamic values if you don’t have to.

 

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

What is Synthetic Monitoring? (2024 Guide)

Posted on 04/18/2024

I've found over the years many testers are unsure about what is synthetic ...

SafeTest: Next Generation Testing Framework from Netflix

Posted on 03/26/2024

When you think of Netflix the last thing you probably think about is ...

Top Free Automation Tools for Testing Desktop Applications (2024)

Posted on 03/24/2024

While many testers only focus on browser automation there is still a need ...

Discover Why Service Virtualization is a Game-Changer:  Register Now!