Test Data Generation | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Test Management
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore test data generation for API testing, emphasizing the importance of using realistic data instead of hard-coded values. This approach enhances the reliability of tests by ensuring unique data for each execution. Key Concepts Random Data Generation: Avoid repeating the same data (e.g., names, user details) by generating random data for each test run. FakerJS Library: A powerful tool for generating random information such as names, addresses, and job titles. Implementation Steps Install the FakerJS library into your project using npm: npm install faker Import the library in your test file: import { faker } from 'faker'; Generate random titles using: const articleTitle = faker.lorem.sentence(5); Update your API requests with the generated titles to ensure uniqueness. Creating a Data Generator For more complex objects, create a utility class, DataGenerator.ts , to encapsulate the logic for generating complete article objects: export function getNewRandomArticle() { return { title: faker.lorem.sentence(5), description: faker.lorem.sentence(3), body: faker.lorem.paragraph(8) }; } Call this function in your tests to generate a complete article object with random data. By following these steps, you can effectively generate the necessary test data for your API tests, ensuring that your tests are robust and reliable.
Video Transcript
Hey guys, in this lesson, we're gonna talk about test data generation. So when you work with the APIs, you work a lot with the data. So sending the request to your APIs and processing the information and so on. And what you don't want to do is to copy the same data again and again. For example, hard-coded first name, last name, or some user details, or something like this. You want to make your data as close to real world environment as possible. And for that, you need to generate this data somehow, randomly, and then send as a payload to your API. And in this lesson, I will show you how to generate this random test data. So going back to our application, and let's review the current test that we have, create, update, and delete article. So currently, we are reading our article request object and setting the title to a new article to update. Then we create the article, and then we send a put request to update this article with a new title, new article update modified. Well, this is not very nice that we type the value of the title that we want to use, and here is modified, and this is without modified. What if we just use a random name every time? And also, that way, we'll make sure that if we send the request twice, then a new title will be created, and we're not gonna have the issue. Remember, our API does not allow us to create the same article with the same title, but if we generate a new article, a new title, every time we run the test, then we're not gonna have this issue and potentially the failure related to this execution. So how are we gonna do that? We're going back to the browser, and we will use a very nice library called FakerJS. Super, super helpful and easy to use stuff to generate just absolutely random information, for example, personal information. Generate names, genders, biographies, job titles, and so on. Location, date, finance, commerce, it has tons of stuff for whatever you need. So let's install this library into our project and just use it. So the first step is just copy this line. And by the way, to find this page, just type into Google FakerJS and you will find this page on npmjs.com. So copying this line, going back to the project, and first of all, we need to install it to our project. So terminal, new terminal, and pasting it right here. All right, installation is complete. The next step, going back, we need to import the library, the place where we want to use it, Ctrl C, Ctrl V. And I add this import in the top of the file like this. And that's it, we can use it. So going back to the test right here, let's say I will create a new article title. Article title equals two, then I call a faker library, and then I put dot. And look, I have all those different options that were shown in documentation. For example, if I want to create details about the person, look, I click person, then click dot, and I can put first name, last name, full name, gender, and so on. And if I, let's say, select the first name, and then there are additional options. For example, I can select do I want male first names or female first names, that kind of stuff. Or let me show you another example. Let's say faker.thePhoneNumber. So for example, if you need a phone number, again, there are different options. No, hold on, it's not here. Okay, and it's either email or number. So I select number, and there are also different options. Just by default, it will be the format like this. If human will be dot notation, national will be like this, and international with plus one, and so on. You can select even different format, depends on the requirements for your application, what data do you need. For us, we need to create just a title. And for the title, we need just absolutely random string. And for the random strings, we can use the thing called lorem. If you know new websites, when you start developing new websites, they use this Latin words and just whatever they means just to fill out the gap. So this is what this lorem is about. And I click dot, and here what we need. So let's type some, for example, sentence. And the length we want to have is how many words. So let's make the sentence with the length of five. I'm gonna put five words. That's it, Faker will generate a random sentence with the five words. Then I take this article title, replace it right here, and the title will be assigned to our object. And then I put the same title over here as the assertion. The same thing we're gonna do for the updated. So const articleTitle2 equals, oops, two equals two. And I call Faker again to generate a new title for me. Then articleTitle2, I replace it right here. And then inside of the assertions as well, right here, right here, and right here, all right? So let's run this test, running it, and test passed successfully. So the Faker successfully generated just a random string for us. This random string was validated inside of the assertion right here. And then we generated another random string, updated the article, and updated this here as well. So this approach would work if you need, let's say, update one or two values inside of your object. And you don't need a lot of changes. But if, let's say, you have situation that you need to generate the bigger object. Let's say you need a user details object with first name, last name, a phone number, address, job title, and any other information. If you have situation like that, probably more convenient would be to create a helper function that would create the entire object for you. And then you just read this entire modified object and use inside of your test. So let's create some example like this using the test number one. So currently, we are only updating the title for our article. How about if we update all properties and we create the function that will, every time, will give us new article details with a title, description, and header. And we will send this article every time creating the article. For that, we can create, for example, a new file right here on the details. And we can call it DataGenerator.ts. Inside of DataGenerator, what we need? We need to faker and our JSON object that we want to modify. And then let's create a new function. Expert function getNewRandomArticle, like this. Then we call our object. Let me copy it from here for the simplicity. And by the way, there is a second option how you can create a clone of your object. Instead of using JSONParse and JSONStringify, you can call also method in Node.js. It's called StructuredClone, okay, this guy. And it's a little bit more compact, by the way. The benefit, oops, hold on, hold on, hold on. We need this one, this one, and we can remove this one. The benefit of this approach is that you will save the IntelliSense inside of your object to query through the dot notation all the properties that this object has, so very convenient. And now what we do is that articleRequest.article.title equals to faker.lorem.paragraph. And we're gonna do a paragraph of five, sorry, not the paragraph. Paragraph we're gonna be doing later is the sentence, sentence of five. Then what's next, articleRequest.article.description equals to faker.lorem. And let's do the sentence, I don't know, sentence of three, how about that? And the last one is description, articleRequest.article.body. Okay, we need a body, and for body, we need just more text. So let's select something different from faker, lorem, and it's gonna be the paragraph. Let's see what we have here. So the number of sentences to generate defaults to three. So let's generate, I don't know, let's generate eight sentences, something like this to make it bigger. And after we've done all this, we just need to return this object, articleRequest, like this. So simple and easy. And now we're going back to the test and just need to call this function. So const, let me comment this out. And we're gonna do this const articleRequest equals to, and we call the function that we just created, which is getNewRandomArticle. getNewRandomArticle, like this. And this article automatically was imported into the spec file. Yeah, I believe that's it. So now we just need to update our assertion. This is the article request that we are sending. And instead of hard-coded data right here, we can call articleRequest.article.title. This is what we are expecting in the assertion right here. This is here and this is here. So we replaced all the data completely. So let's run this test first of all and make sure that it is working. Yeah, test passed. Now let me run it one more time without deleting the article. I just want to show you in the application that actually data was generated. So run it one more time. Article should be created, going back to application, refresh, and here we go. This is the randomly generated article by our API. And if I open it, look at this. It have a pretty big paragraph of just weird random stuff. But that's all we need. That's all we need for our testing, just validate that we have a nice paragraph for text or the long title for our article, and it works just fine. Well, that's pretty much it, guys. That's pretty much it. That's how easy you can generate the data for your test. Generate whatever you need and just choose the approach that you like. So if you need just to generate a few values inside your test, call Faker directly inside of your test and generate here and there those values. If you need to generate just a bigger object, create a utility class which is DataGenerator. And inside of DataGenerator, put the helper functions that will generate a bigger test objects with a random data for you that you later gonna use inside of your payloads. All right, that's it, guys, and see you in the next lesson.