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.
If you use maven, to include jFairy in your project add the following dependency:
compile group: ‘io.codearte.jfairy’, name: ‘jfairy’, version: ‘0.5.9’
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:
31 Summer Place
New York 23795
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:
Herrenkrugstraße 169, 151
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:
…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:
Or you can generate IP numbers:
Which can give you IP number like one below:
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!