So far whenever we've been running Karate Tests because we're not using the Karate plugin to execute our tests, we're running through Maven. But each time it's running for every single test. Now for us, that's okay because we don't have too many tests yet. It's not too much of a problem to run them all. But soon, once your framework goes, you're going to be able to want to execute single tests. Likewise, you might want to run a build that only has certain tests included in it, but you still want to have lots of other tests in your code-based. The way that we can get around this is that we can use what's called Karate Tags to only execute certain tests. So let's have a look at how we can do that now.

 
I'm here in my main Todos feature file. We can see here that we've got these two scenarios. So we've got two scenarios. One for getting all the Todos and one for doing the basic Todo flow. So if I only wanted to run, for example, these Get all Todos. What I can do is I can just add a certain tag above that. I can just add a tag, for example, called debug. Suppose it's not, I'm just making up this debug tag. I could call it anything, I could call it foo, bar, etc. I'm just calling this the debug tag. It's not a special tag. It's just I'm just choosing to call it debug. Now, I've got this debug tag in place. If I want to run just the test with just this tag, I can do mvn clean test. What I do is -DKarate.options equal double brackets, dash dash tags and the name of the tags in this case, just debug. Show you at the double quote there as well. Now, again, when I execute this maven command, now it's just going to run the tests with the debug tag. Let's go ahead and try that out. Now if we look back at the test result, which report is only executed, this one test, that one test that did the get all the Todos. It's another thing that we could do with tags. We've put this tag just above the scenario. But if we want to just execute everything within the feature, I could take that tag out and I could put it above the feature instead. If I put the tag here above the feature, if I go back into maven again. I'm going to execute the same commands of mvn clean tests with the debug tag. And this time it's run all of the tests or the two tests that we have in the Todos feature.

What might be easiest if we open up the Karate reports? Let's just go over to the IDE. Here I am in the IDE and we can see at this time now when our Karate report only includes this Todos feature and the two tests that got run in there. It doesn't include the other feature file that we have which does the updating. Now there is another special tag that Karate has that you can use if you just want to ignore certain tests. So often again, once your code base grows to have many hundreds of tests in there, you'll often want to ignore certain tests for whatever reason if you're debugging or you just want to take it out of the CI/CD pipeline for a while. What you can do with that is you can add in a special tag called @ignore. So what I'll do is I'll add the ignore tag above the feature file. What it will do now is that it will ignore all of the tests within this feature.

Now, equally, I could add if I wanted to ignore just on scenario, I could ignore just that scenario. But because I'm ignoring the entire feature, it should ignore every test within this feature file. Let me go ahead and run this. So again, for this time I don't need to specify a tag. I can just do mvn clean tests because the @ignore tag is a special tag we don't need to specify it. I'm going to mvn clean test. And I would expect when we look at the results report the only the update Todos features tests should get run. It looks like there's only been one feature for running from just looking at the logs. Let's just go back over to the Karate report. By refresh, we can see that this time we only executed the update Todos feature file.

So we ignored this other one here. Okay, that looks good. I'm actually going to take the ignore out from this one. And this time I'll put ignore on the update Todos feature instead. Oops, not debug, ignore. There we go. I've just got the ignore tag this time on the update one.

Let me just run this once more just to check if that's working. Let's check out the results report. This time it only ran the Todos feature test. We didn't use the updates one this time. That's basically how you can use Karate tags within your framework to only run certain tests.

Comments are closed.

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