Test Tags | Bondar Academy
Course: Cypress UI Testing with JavaScript
Module: Advanced Features
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore the concept of test tags in Cypress, particularly useful for managing large test suites with multiple spec files. Test tags allow you to run specific tests across various spec files by marking them with designated tags. Configuring Test Tags To use test tags, we need to install a plugin called Cypress-grep . Here are the steps to configure it: Install Cypress-grep: Open the terminal in your Conduit Cypress project and run the installation command. Add Configuration: Insert the necessary lines into e2e.js . Setup Node Events: Add the required lines into setupNodeEvents . Basic Usage Once configured, you can use the grep command to target tests by their names or tags. For example: npx cypress run --env grep=smoke This command executes only the tests marked with the smoke tag. You can also assign multiple tags to a test by using an array. Advanced Filtering To exclude tests from the run, use the flag grep-filtered-spec=true . For complete isolation of tests, use grep-on-mid-filtered=true . Combining Tags You can combine tags in various ways: Run tests with either smoke or critical tags. Run tests with smoke but not slow tags. Burning Tests A useful feature is the burning test , which allows you to validate test stability by executing a test multiple times (e.g., burn=5 ). In summary, using the Cypress-grep plugin provides a reliable way to filter and execute only the necessary tests, enhancing test management efficiency.
Video Transcript
Hey guys, welcome back, and in this lesson, we will talk about test tags. So sometimes, when you have a big test suite with multiple spec files, you may need to run a separate test across multiple spec files. But how would you target those? And in these situations like this, using test tags can help. So you just mark your specific test by specific tags. And then in the command line, you call the tags to execute only those tests. So let's configure test tags in Cypress. For that, we will need to install a separate plugin called Cypress-grep. Very popular one and handles everything very nicely. So let's configure this. First, I need to install this into the project. And we continue using Conduit Cypress project, opening terminal and installing Cypress-grep. And that's it, moving on. The next step, we need to add these two lines into e2e.js. E2E, adding these two lines, all right. Then the step three, we need to add these two lines into setupNodeEvents. Going back, configuration and setupNodeEvents. And I believe that's it, yeah, pretty much three steps. And then basic usage. So you can use grep either by the text of the test name, or you can add the tags like this, and then target them by text. So let me show you this example. Let's say we want to run the first test. And then I need to add the object over here with additional properties. And I call the tags. And then assign whatever name of the tag that I want to use. Say it will be a smoke test, like this. And let's say for the second test, I want to add text as well. We already have the object, so we just need to add a second argument to this object, which is a text. And you can also assign multiple tags to the same test. Let's say I will assign them as a smoke. And second tag will be related to the feature. And this is the test that's testing the Favorites button. Let's call it Likes. So we're gonna test Likes feature. And when you provide multiple tags, you need to put them as an array. And that's it, npx cypress-run-env. And then I need to call this grep tags smoke. This is one of the tags that we have. And just execute. And so right now, Cypress should execute only two tests for us. So it's running the tests, and here we go. It's done. Look, it's executed two tests for the smoke test, for the first test and modify API response, but it marked the rest of the test as skipped. If you want to exclude those tests completely from the test run, so they will not be displayed for the other spec files. You can use this flag, grep filtered spec true. So it will filter other spec files. So I put comma and like this. And so it will not display the second spec file that we have. And it will show only the spec file where the tags are, so working with API. But it's still showing that three tests are pending. If you want to remove even that completely and want to run just these two tests and not touch the other tests within the spec file at all, you can use other flag which is grep on mid filtered true like this. So let me replace this with this flag. And now it will run only these two tests only. So running the test. Here we go, you see only these two tests executed. And if I do, for example, instead of smoke, I will do what we have likes, right? So likes and run this test one more time, then only one test will be executed across this spec file. Yeah, only one test is executed. So pretty simple. You can look into documentation. You can actually combine different options how you can run the tags. For example, if you want to run either smoke or critical, you put the space. If you want to run two types of tags at the same, two or three, you just smoke plus critical. Or if you want to run smoke but not certain type of tech, you run like smoke but not slow, that kind of stuff. You can combine running tags by the certain word in the title of the text and the specific tag, you can also do this. So these are the options. And in the advanced feature, I like this option of the burning test, so burn five. This is useful to validate if your test is stable enough. For example, you created a certain test and then run by the tag and put the property burn five. And it will repeat the same test execution five times, just to make sure that this is stable test and it is not flaky. All right, so simple to use. Check other options and a very reliable way to filter your test and run only the test that you need for your test execution using this plugin, cypress-grep. All right, that's it guys, and I'll see you in the next lesson.