Data Driven Testing

 One final thing that I wanted to show you is how we can do data-driven testing in Karate. So at the moment when we're making our call, the Post call to create new Todos, each time that we go through that, we have to write out all of the codes like the Post, etc.. And so it's quite a labor process to have to go through and create each different Todo. What we can actually do is we can actually template this in Karate just so that we can make lots of these different calls in a data-driven way. Let me show you an example of how we can do that now. Back here over in the code base again, I'm just going to close down these two here. We don't need those for th moment. And in our Todos folder, let's create a new file. Let's call it datadriven.feature. Give our feature a name, let's call it Data driven testing example. Let's also make sure we add in the background as well, just so that we have the URL. That's just * URL and the API URL. Remember that API URL is the one that we're defining in the Karate config file. Now, instead of calling it a scenario, we can actually call this a scenario outline. And this is the difference when you want to do data-driven testing. Let's go ahead and fill in this outline now. So it's going to be similar to what we already done for when we're making a Post call. Let's just do star request for a payload, remember, it needs to have a title. Normally we would put in the title here like first or second. But what we're actually going to do this time is we're going to parameterize this and we're going to replace the parameter each time. So to do that, it has to hash and the brackets. Then we're going to call our parameter title and we'll be setting this data in just a moment. And let's also add a complete as well.

Let's just set all of these tasks to be false for the moment. That's what the rest of our Karate codes. This time we just do * method post and let's match on the response as well. So we're going to make sure for our response that we get some JSON back. It's going to have an ID, not going to know exactly what the ID is, but we're going to note that it's Strings, which is two hash string title. We do know what that's going to be. And the title is going to be whatever is set in our title parameter to just do hash title. And we also know that the completes will be false as well. There we go. Also check that the status is 200.

Okay, that looks good. Now that we've got all outlined. Now we need to put some data into that outline. So let's see how we can do that. To do that, we just type the word examples. Let me do a pipe. Then we put in our parameters.

So the main one that we need is the title. And that's going to be the title that we're specifying above. And that's also put in another one as well, just for iteration, so that we can count our iterations through the scenario outline. Let's go down now. We can start putting in our test data. The first title will just be one that's going to be on iteration one, one will be two and three. Okay, that looks good. That's also then print out this iteration since we're capturing it. Say * print, say finished iteration and will print out the iteration. Now, this is going to execute quite quickly just because there are only three different calls that we're making. What we might want to do, though, when we're calling this is that we might want to have as Post period at the bottom so we can basically define a sleep just so that Karate will pause for a few seconds just because it would make it a bit easier to just watch this test executing, basically. 

So let's have a look at how we can quickly add that into it. Add a sleep in Karate in the background block here. I'm just going to do * Def sleep. For that sleep, we just call JS function say function, pause. And then we can just call Java.lang.Thread.sleep for the length of the sleep if we want that to be the pause times 1000. So don't worry if you don't understand this code here is basically just calling Java. It's just making sure that the thread will sleep for this amount of time that we're going to specify and then we times it by a thousand because the sleep is going to be in milliseconds. We can now call that sleep functions. We just do * Sleep. Let's just say we want to sleep for five seconds.

So that five, that's kind of called a sleep function up here. It's going to parse in the value of five. And it's just going to call pause for 5000 milliseconds or 5 seconds. Okay, That looks good. We can go ahead now and run this feature file. What I'll do is I'll just add the debug tag up here just so that we run just this single feature file. Open up my terminal again and I'm going to do mvn clean test. And I'm going to execute the tests with this debug tag. Okay.

So we can see here, that we've got logs saying finished iteration one, seems to be pausing for a few seconds. Now it's done with iteration 2. And now it's done with iteration 3. Pausing again for a few seconds. And now the test is finished and everything's passed successfully.

So that's a pretty basic example of how you can do data-driven testing in Karate. If we just jump over to the Todo application again and refresh that, we can see it's created the three different tasks one, two, and three for us.

Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}