Test Data Generator | Bondar Academy
Course: Cypress UI Testing with JavaScript
Module: Advanced Features
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore the importance of test data in application testing and how to efficiently generate it using the Faker library. Why Use Dynamic Test Data? Hard coding test data can lead to: Lack of diversity in test scenarios Difficulties in management and updates Time-consuming processes To mimic a real production environment, it is essential to use dynamic data generation. Introducing Faker The Faker library is a popular tool for generating various types of test data, with nearly 10 million weekly downloads on npm. It can create data related to: People Locations Dates Finance Commerce Strings and numbers Using Faker in Your Project Install the Faker library using npm: npm install faker Import Faker in your test file: import Faker from 'faker'; Utilize Faker to replace hard-coded values: const title = Faker.name.findName(); Generate a description and body using Faker methods: const description = Faker.name.jobTitle(); const body = Faker.lorem.paragraphs(10); Running Your Tests After implementing Faker, run your tests to see dynamic data generation in action. Each test run will produce unique values, enhancing the robustness of your testing process. Explore the Faker library for a wide array of options to improve your test data generation!
Video Transcript
Hey guys, welcome back, and in this lesson, we will talk about test data. So quite often when you run your test, you need a lot of test data to fill out in your application. For example, people information, I don't know, address, phone number, location, some random text, some whatever. And if you just hard code this test data, for example, in our app when we fill out the details about the article, hard coding this data every time, well, it's not really good, it lacks diversity, and it's not how it works in real production environment. And your goal is mimic your real environment as close as possible. And also, hard coding the data, difficult to manage, hard to update, and takes time. And to manage this more conveniently, there is a perfect library available called Faker. And this library can generate a tons of test data for you, absolutely free and easy, so let me show you how to use it. So on the npmjs.com, we are looking for this npm library Faker, and it is very popular. So look, almost 10 millions weekly download, and you can generate data about the people, location, date, finance, commerce, number and string, localization, anything, so a lot of data over there. So let me show you how to use it. First of all, we need to install it. Scrolling down, I'm copying this line, and I'm going back to the project conduit. We will automate this scenario, delete article. So previously, we were hard coding title, description, and body in this test. But instead, let's replace it with a dynamic data. So I paste this line in the terminal to install Faker library into the project. After you did this, you will see Faker will be added as a dev dependency. So the package is installed. The next step, we need to import this library into our test file. So I'm copying this import, going back and pasting it in the top of the file. And that's it, now we can use this Faker library inside of the test. So let's do this, delete the article. So instead of hard coding the string, I just type Faker and put dot. And look, how many options do I have? Airline, animal, book, color, commerce, company. Like a lot of options, a lot of options to explore. Let's say we will talk about the person. So where is the person? So I type the person, dot, and then more information, biography, first name, gender, job, area, description, last name, and so on. So let's say we will select full name, for example. It will be the title for our article. Then for the description, what we're gonna choose. Let's do Faker, then do person one more time. And let's say for the description, we will put, I don't know, job title. Let's do something like this. And for the body, for the body, I would use Faker one more time, dot. And let's type some random stream. And for that, we have this thing called lorem. So I call lorem, and then I want to type the paragraph. So in the paragraph, I can provide the details, additionally customize what I want to do with this paragraph. So we hover over every method head as a description how you can use this method. For example, the method paragraph generates a paragraph with a given number of sentences and the default length of three. So if we will not provide any other arguments, then just three sentences will be generated. Let's say that we want to generate ten sentences for our paragraph. And I guess that's it. And there is only a little problem, because previously in the test, we hardcoded the titles here, here, and here. Then we need to create a constant to create this new title as a person full name, and then reuse this constant throughout the test. Because if I call this faker over here like this, it will generate a completely new full name for me. But I don't want it, I want to use the same full name across entire test, so this will not work. And for that, I will move this to the beginning of the test and create a new constant, const title of the article equals to, and this is my title. And then I copy this constant and replace everywhere where I want to use it. So create, then validate that it was created, then click on the article by its title, and then validate that it does not exist anymore. All right, so let's check if it's working or not. So terminal, new terminal, npx cypress open, and running this test. I think I need to put it.only as well, it.only, like this. So e2e, electron browser, and we're running the test, and that's it. Article was created, so let's see. I scroll a little bit up, and the article with title Mr. Terence Kessler was created. And if I go back a little bit, so let me find where is before, after, and here we go, highlights. And this is the body that was generated. Look, a completely random unreadable string, but technically what we needed is just ten sentences. Here we go, it's generated for us ten sentences. And also we should see somewhere a short description for the job, and look, future security director. So it's generated it for us. And if I try to run this test one more time, a completely new values for this test run will be generated. So right now we have a completely new person with a completely new, different random string, and so on and so forth. So guys, this is how you can use it, explore the Faker library. Use it for your test data generation. You see, very easy to use and tons of options for the test data. All right, that's it guys. I'll see you in the next lesson.