Skip to main content

Generating Test Data – jFairy

Test data has been always an issue. If you are running selenium or backend automated tests based on user-related scenarios, in order to make your tests more efficient you need to provide unique and realistic user test data. There’re many ways to deal with this: dumping samples of production databases, writing your own data generators or using the very same data in every test run and cleaning database afterwards.

In this post I want to write about small and handy Java library for generating fake test data – jFairy. Since the library is super-simple to use, this post is just the shout-out for the nice tool I’ve been using in many different automation projects and I hope I’ll put a spotlight on it for my readers.


Installation 

If you use maven, to include jFairy in your project add the following dependency:

<dependency>
    <groupId>io.codearte.jfairy</groupId>
    <artifactId>jfairy</artifactId>
    <version>0.5.9</version>
</dependency>

For gradle:

compile group: 'io.codearte.jfairy', name: 'jfairy', version: '0.5.9'

Use examples 

First thing to do is to call static method create() to create Fairy class instance:

Fairy fairy = Fairy.create();

From now on you can generate test data. Let’s start with creating random person:


This prints random person’s data, like one following:

Gavin Rodgers 
31 Summer Place 
New York 23795 
gavin.rodgers@gmail.com

As you can see email data matches person’s first and last name. You can also specify nationality of generated person’s data. The moment I’m writing this post you can use locale: en, de, es, ka, pl, sv and zh. Try pass locale as follows:

Fairy fairy = Fairy.create(Locale.forLanguageTag("de"))

This will print analogous test data to previous one, but for german citizen:

Zacharias Wohlgemuth 
Herrenkrugstraße 169, 151 
18421 Brannenburg 
zachariasbacher@yahoo.de

The feature I like the most about jFairy is the ability to specify whether you want to generate male or female person’s data. In the below example we’re generating test person who is female with age between 20 and 30. This time I created simple Spock test to prove it:


This time jFairy gave us Camila:

Camila Melendez 
29

…and the test passes, so isFemale() method returned true.

You can go further with using jFairy than creating fake personal data. You can create entities such as company’s data:


Which gives us the brilliant startup idea:

MemorTech Limited 
office@memortechlimited.biz 
memortechlimited.biz 
http://www.memortechlimited.biz 
26-0006221

Or you can generate IP numbers:


Which can give you IP number like one below:

149.234.232.17

Summary 
The goal of this post isn’t to describe all of jFairy features, but to bring your attention to the project. Since the library is quite small I encourage you to explore its implementation for your own – it has more to offer. Are there any cons? I wouldn’t call it con, but generating credit card numbers is challenging and repetitive task (control number) and Fairy.creditCard() gives us only cardVendor and expiryDate. Well, sounds like a pull request idea!

Popular posts from this blog

Notes after TestingCup 2018

On May 28-29th I attended TestingCup conference in Łódź. Having quite unique perspective: this was my second year in row as a Speaker at this conference I want to share some thoughts on the event. Dust has settled, lets go! 



Championship Originally TestingCup is a software testing championship. Wait, what? Yes, the formula is unique: teams and individuals from all around Poland are competing in finding the most bugs and defects in specially prepared application - Mr. Buggy. I don’t have specific data, but since this year’s conference was all english I guess competitors were not only from Poland. As a spectator, I must say that the whole competition looked very professional. There were team shirts and names, podium and trophies (gold cup and cash). 
Some cons? Testing championship is held at the first day of the conference. So this is already a conference, but if you’re not taking part in the championship… there’s not much to do, since all the talks are in the next day. Organizers are aw…

REST-Assured framework overview

In modern software development, REST services becomes most popular choice for implementing distributed and scalable web application. They are light and easy to maintain, which results in faster and more effective implementation and system integration.
I recommend you also my other posts about REST-Assured and building microservice’s test automation frameworks: REST-Assured: going deeperBuilding microservices testing framework
With the increase popularity of RESTful services, there is a need for fast and lightweight tool for REST webservices testing automation. One of the most popular choice is Rest-Assured framework from Jayway. It introduces simplicity of testing web services from dynamic languages like groovy or ruby to java. In this post we will get our hands dirty and write automatic test in Rest-Assured framework.
In order to create complete implementation of automated tests in Rest-Assured framework, we need to write our code against some example API. We’ll use standalone Wiremock m…

Building microservices testing framework

RESTful services and microservice architecture in general are big trends in the industry right now. I’ve noticed it also from this blog’s traffic analytics, where topics related with REST testing get the highest interest. Therefore I’ve decided to create some kind of example framework for RESTful APIs functional testing. Project is based on REST-Assured as a services client, Spock as a testing framework, Groovy and Gradle. You can clone full repository from my github. Tests are run against Wiremock API described in this post. Please consider this project as a kind of bootstrap, since it’s an example, not full-blown test framework. Ok, so as Linus said – talk is cheap, show me the code!
Dependencies Usually, first thing for me after importing new project is opening build.gradle (or pom.xml, in case of maven). In our example, the most important dependencies are REST-Assured and Spock. We’ve also Jackson, for serializing objects, Logback for our logs and snakeyaml for reading .yaml files,…