Test data is essential for both manual and automation testing. However, test data changes in manual testing aren’t as concerning as in automation testing. That’s primarily because, in manual testing, the testers can change/create the data, they are also aware of the data changes and hold enough application knowledge. However, when it comes to automation testing, the tool cannot have this kind of knowledge or information. And so, it becomes a tedious process to generate test data.
Automation Testing Goals
The primary goal of automation testing is to run the same tests/scripts across different environments and then managing the test data. Especially, when the application accepts unique data and we cannot hard code the values. When unique test data is fed into the application depending on a third-party API, a simple solution can be generated. Using this solution, test data can be managed effectively as the data does not change for each iteration.
Third-party Test Data Generators
There are third-party unique test data generators available in Java and C# which will also be discussed in this blog. Normally to generate the unique test data, we can use random classes available in Java and C#. Using these classes, we can get specific test data by generating a random unique number that can be appended to a string. This is extensively useful when you automate your application with Selenium / Appium using Java or C# language.
Sample Program Using Random Class in Java
import java.util.Random; public class RandomTestDataGenerator { public static void main(String[] args) { Random r = new Random(); String fullName = "fullName" + r.nextInt(); String firstName = "firstName" + r.nextInt(); String lastName = "lastName" + r.nextInt(); String streetAddress = "address" + r.nextInt(); long phoneNumber = (long) (Math.random() * 100000 + 3333000000L); String email = "email" + r.nextInt() + "@gmail.com"; System.out.println("The Full Name is : " + fullName); System.out.println("The First Name is : " + firstName); System.out.println("The Last Name is : " + lastName); System.out.println("The Address is : " + streetAddress); System.out.println("The Phone Number is : " + phoneNumber); System.out.println("The EMail is : " + email); } }
Sample Program Using Random Class in C#
using NUnit.Framework; using System; namespace CheckFrameworkWithNunit3 { [TestFixture] public class TestDataGenerator { [Test] public void RandomTestDataGenerator() { Random r = new Random(); String fullName = $"FullName{r.Next()}"; String firstName = $"FirstName{r.Next()}"; String lastName = $"LastName{r.Next()}"; String address = $"Address{r.Next()}"; String phoneNumber = r.Next(1000000000).ToString().PadRight(10, '0'); String email = $"Email{r.Next()}@gmail.com"; Console.WriteLine("The FullName is: " + fullName); Console.WriteLine("The FirstName is: " + firstName); Console.WriteLine("The LastName is: " + lastName); Console.WriteLine("The Address is: " + address); Console.WriteLine("The EMail is: " + email); Console.WriteLine("The MobileNumber is: " + phoneNumber); } } }
Using random class, test data is not readable or meaningful as we are generating some unique random numbers and appending to the hard-coded string. Therefore, the solution to this problem is to use other third-party APIs available in Java and C#.
Java-based APIs
- Faker API
- jFairy API
- Fabricator API
Faker API in Java
Faker API will generate unique fake/test data. Test data generated using this API can help testers in testing the application when there is a need for unique test data. By using Faker, we can generate distinct categories of data (i.e., address, color, date & time, name, phone number, etc.). Further, test data generated can be country specific as well. Please click on the link for detailed information.
Sample Program using Faker API in Java
import java.util.Locale; import com.github.javafaker.Faker; public class FakerTestDataGenerator { public static void main(String[] args) { Locale locale = new Locale("en-IND"); // It will generate India specific data. Faker faker = new Faker(locale); String fullName = faker.name().fullName(); String firstName = faker.name().firstName(); String lastName = faker.name().lastName(); String address = faker.address().streetAddress(); String phoneNumber = faker.phoneNumber().phoneNumber(); String email = faker.internet().emailAddress(); System.out.println("The Name is : " + fullName); System.out.println("The First Name is : " + firstName); System.out.println("The Last Name is : " + lastName); System.out.println("The Address is : " + address); System.out.println("The Address is : " + phoneNumber); System.out.println("The EMail is : " + email); } }
Jfairy API in Java
jfairy API will generate unique fake/test data based on Wikipedia, to help us generate random unique fake/test data which can be fed to the application for testing. Click on the link for detailed information.
Sample Program using JFairy API in Java
import io.codearte.jfairy.Fairy; import io.codearte.jfairy.producer.person.Person; public class JfairyTestDataGenerator { public static void main(String[] args) { Fairy fairy = Fairy.create(); Person person = fairy.person(); String fullName = person.getFullName(); String firstName = person.getFirstName(); String lastName = person.getLastName(); String streetAddress = person.getAddress().getAddressLine1(); String phoneNumber = person.getTelephoneNumber(); String email = person.getCompanyEmail(); System.out.println("The Name is : " + fullName); System.out.println("The First Name is : " + firstName); System.out.println("The Last Name is : " + lastName); System.out.println("The Address is : " + streetAddress); System.out.println("The Address is : " + phoneNumber); System.out.println("The EMail is : " + email); } }
Fabricator API in Java
Fabricator API will generate unique test/fake data especially for the automation scripts/tests. Further, it also generates various categories of data (i.e., finance, internet, location, and mobile, etc.). Click on the link for detailed information.
Sample Program using Fabricator API in Java
import fabricator.Contact; import fabricator.Fabricator; public class FabricatorTestDataGenerator { public static void main(String[] args) { Contact contact = Fabricator.contact(); String fullName = contact.fullName(false, false); String firstName = contact.firstName(); String lastName = contact.lastName(); String address = contact.address(); String phoneNumber = contact.phoneNumber(); String email = contact.eMail(); System.out.println("The Full Name is : " + fullName); System.out.println("The First Name is : " + firstName); System.out.println("The Last Name is : " + lastName); System.out.println("The Address is : " + address); System.out.println("The Phone Number is : " + phoneNumber); System.out.println("The EMail is : " + email); } }
C#Based APIs
- Faker API
- Bogus API
Faker API in C#
Faker API in C# will generate different sets of unique test data like names, addresses, phone numbers, etc. Click on the link for detailed information.
Sample Program using Faker API in C#
using NUnit.Framework; using System; namespace CheckFrameworkWithNunit3 { [TestFixture] public class TestDataGenerator { [Test] public void FakerTestDataGenerator() { string fullName = Faker.Name.FullName(); string firstName = Faker.Name.First(); string lastName = Faker.Name.Last(); string address = Faker.Address.City(); string phoneNumber = Faker.Phone.Number(); string email = Faker.Internet.Email(fullName); Console.WriteLine("The FullName is: " + fullName); Console.WriteLine("The FirstName is: " + firstName); Console.WriteLine("The LastName is: " + lastName); Console.WriteLine("The Address is: " + address); Console.WriteLine("The EMail is: " + email); Console.WriteLine("The MobileNumber is: " + phoneNumber); } } }
Bogus API in C#
Bogus API in C# will generate unique fake/test data. By using this API, we can generate distinct categories of data (such as addresses, company, date, finance, and internet, etc.). Additionally, test data specific to a country can also be generated.
Sample Program using Bogus API in C#
using NUnit.Framework; using System; namespace CheckFrameworkWithNunit3 { [TestFixture] public class TestDataGenerator { [Test] public void BogusTestDataGenerator() { string fullName = new Bogus.DataSets.Name("en_IND").FullName(); string firstName = new Bogus.DataSets.Name("en_IND").FirstName(); string lastName = new Bogus.DataSets.Name("en_IND").LastName(); string address = new Bogus.DataSets.Address("en_IND").City(); string phoneNumber = new Bogus.DataSets.PhoneNumbers("en_IND").PhoneNumber(); string email = new Bogus.DataSets.Internet("en_IND").Email(); Console.WriteLine("The FullName is: " + fullName); Console.WriteLine("The FirstName is: " + firstName); Console.WriteLine("The LastName is: " + lastName); Console.WriteLine("The Address is: " + address); Console.WriteLine("The EMail is: " + email); Console.WriteLine("The MobileNumber is: " + phoneNumber); } } }
Evoke’s QA and Testing Services
Evoke offers world-class quality assurance & software testing services which accelerate and improve software application performance across the enterprise. Our software testing services incorporate a blend of modern tools and test automation frameworks that help enterprises to improve their software quality and enhance the performance of their software applications. With an established quality assurance practice, we have been helping global enterprises gain predictability, increase performance, significantly reduce the total cost of testing and achieve high ROI.
To know more about Evoke’s customer-focused IT services, please connect with us at +1 (937) 660-4925 or contact us online. Alternatively, you can write to us at sales@evoketechnologies.com.
Author
Krishna Sakinala is a Senior Quality Analyst at Evoke Technologies. He is technically adept on Selenium automation with Java and C# languages. Krishna is keen on exploring different types open source automation tools and frameworks. |