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

Testing Asynchronous APIs: Awaitility tutorial

Despite the growing popularity of test automation, most of it is still likely to be done on the frontend side of application. While GUI is a single layer that puts all the pieces together, focusing your automation efforts on the backend side requires dealing with distributed calls, concurrency, handling their diversity and integration. Backend test automation is especially popular in the microservices architecture, with testing REST API’s. I’ve noticed that dealing with asynchronous events is particularly considered as challenging. In this article I want to cover basic usage of Awaitility – simple java library for testing asynchronous events. All the code examples are written in groovy and our REST client is Rest-Assured. Synchronous vs Asynchronous  In simple words, synchronous communication is when the API calls are dependent and their order matters, while asynchronous communication is when the API calls are independent. Quoting Apigee definition: Synchronous  If a

Rerun Flaky Tests – Spock Retry

One question I get asked a lot is how you can automatically rerun your test on failure. This is a typical case for heavy, functional test scenarios, which are often flaky. While test flakiness and its management is crucial and extensive matter itself, in this post I want to give a shout to the extremely simple yet useful library: Spock-Retry. It introduce possibility to create retry policies for Spock tests, without any additional custom-rules implementation – just one annotation. If you are not a fan of Spock testing framework and you prefer JUnit – stay tuned! I will post analogous bit about rerunning JUnit tests soon. Instalation  If you are an maven user, add following dependency: <dependency>       < groupId > com.anotherchrisberry < /groupId >       < artifactId > spock-retry < /artifactId >       < version > 0.6.2 < /version >       < type > pom < /type >   </dependency> For gradle users:

Performance Testing – Vegeta Attack!

Performance testing is crucial field of modern software development. Taking into account that in today’s world majority of app’s communication is web-based, it turns out to be even more important. However, it still enjoys less interest than automated functional testing, and publications on load testing subject usually focus on mature and complex tools like JMeter or Gatling. In this post I’d like to introduce command line usage of a super simple and lightweight tool for performance testing of HTTP services, which is Vegeta. Who is Vegeta  Besides Dragon Ball’s character, Vegeta is a simple load testing tool written in GO, that can be used both from command line and as an external library in your project. In order to use Vegeta, download executables from here . It is recommended to set environment variable for ease of use.  For Mac users, you can download vegeta directly from Homebrew repositories: $ brew update && brew install vegeta Vegeta’s Arsenal  Usage