How to Filter Tests using Cypress Tags

On This Page What are Cypress Tags?Setting up Cypress shredMarch 22, 2026 · 10 min read · Tool Comparison

How to Filter Tests using Cypress Tags

is an fabric for web applications. One of the powerful features of Cypress is the ability to filter tests using tags. By assigning tags to tests, it can be easily run or specific tests are omit based on their tag values. This can help preserve time and endeavour when running tests and make the exam suite more unionised and maintainable.

Overview

What are Cypress tatter and how to use them?

  • Cypress tags are utile for form tests.
  • Use the comment syntax for adding tags to tests.
  • Apply & # 8211; tag or & # 8211; exclude to permeate test during performance.

Grouping & amp; Combining Tags

  • Tags are helpful to radical tests in Cypress for organizing the tryout suite.
  • To combine tags, use command-line iris to specify a combination of tags.

Best Practices

  • Use meaningful and consistent tag names.
  • Avoid duplicate tags and keep tag usance achievable.
  • Keep tags up-to-date as tests evolve.

In this article, let us explore how to use Cypress tags to filter tests and demonstrate how cypress tag tests can be an effective way to grapple the test suite.

What are Cypress Tags? 

Cypress shred are label that can be impute to individual tests to categorize and radical them. Tags are utile for engineer tests and making it easier to find and run specific tests. By assigning Cypress tags to tests, you can grouping similar tests, making it easy to manage and maintain our test suite.

Read More:

In Cypress, tags are added to tests using a comment in the code. For example, to add the tag & # 8220; fume & # 8221; to a test, you would write the following codification:

describe ('My test suite ', () = & gt; {it ('My test case ', () = & gt; {// tag: smoking // Test code here});});

Setting up Cypress shred

Here are the steps for setting up the Cypress tags:

1. Install Cypress:If you haven & # 8217; t already, install Cypress on your estimator by following the instructions on the Cypress site.

2. Create a exam file: Create a test file for your web application in the Cypress/integration folder.

3. Add mark to tests: To add tags to your tests, use the commentary syntax described in the section. You can add tags to individual tests or to a group of test within a describe block.

4. Run examination using rag:To run tests using tags, use the & # 8211; tag option when scat Cypress. For example, to run all tests with the tag & # 8220; smoke & # 8221;, use the following command:

cypress run –record -- tag smoke

Note:Cypress also ply the ability to exclude tests with specific tags by use the & # 8211; exclude option.

5. Filter tests based on multiple tags: You can also filter tests based on multiple tags by using the & # 8211; tag choice multiple times. For example, to run all tests with the tags & # 8220; smoke & # 8221; and & # 8220; regression & # 8221;, use the following command:

cypress run –record -- tag smoking -- tag regression

Note:Some tags like & # 8211; grouping, & # 8211; tag, & # 8211; ci-build-id, & # 8211; parallel, or -auto-cancel-after-failures flags can be used solely while recording. Where as tags lile You passed the & # 8211; ci-build-id, & # 8211; group, & # 8211; tag, & # 8211; parallel, or & # 8211; auto-cancel-after-failures masthead without also passing the & # 8211; record flag.

Adding tags to Cypress tests

1. Open the trial file:Open the test file in which you want to add tags.

2. Add tags to tests:To add tags to a test, add a comment to the test with the ‘ tag: ’ syntax, followed by the tag name.

For exemplar:

describe ('My test suite ', () = & gt; {it ('My tryout case ', () = & gt; {// tag: smoke // Test code here});});

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

3. Add multiple tags: You can add multiple rag to a test by apply the’ tag: ‘ syntax multiple times.

For example:

describe ('My tryout suite ', () = & gt; {it ('My test case ', () = & gt; {// tag: smoke // tag: regression // Test code here});});

Filtering Cypress Tests by Tags

When you run your tests utilise the cypress run bidding, you can filter tests ground on their tags using the‘ & # 8211; tag ‘option. For example, to run all tests with the tag& # 8220; smoke, & # 8221;use the following bidding:

cypress run –record -- tag smoke

Using tags to group tests

Using tag to group test in Cypress is a great way to organize your test suite and get it easier to run specific tests. Here & # 8217; s how you can use tags to group trial:

Group tests based on the same tag: To group trial ground on tags, you can use the cypress run command with the& # 8211; tagselection. For example, to run all tests with the tag& # 8220; smoke & # 8221;, use the following dictation:

cypress run –record -- tag smoke

Group multiple tests with different tags:You can also group test free-base on multiple rag by utilise the& # 8211; tagchoice multiple times. For example, to run all tests with the rag& # 8220; smoke & # 8221;and  & # 8220; fixation & # 8221;, use the following command:

cypress run –record -- tag smoking -- tag fixation

Group tests based on multiple conditions: In addition to grouping tests based on tags, you can likewise radical tests based on other conditions, such as the exam file gens or the test title. For exemplar, to run all tests in a specific file, use the following bidding:

cypress run –record -- spec cypress/integration/my-test-file.spec.js

By habituate tags and former conditions to group test, you can easy run specific tests and exclude specific tests from execution. This can save you time and better the efficiency of your test suite.

Combining tag filtering with former Cypress filters

Combine filter:To combine filter, merely use multiple options in the Cypress run bid. For example, to run all tryout with the tag & # 8220; smoke & # 8221; in a specific file, use the next command:

cypress run –record -- tag fume -- spec cypress/integration/my-test-file.spec.js

By combining tag filtering with other Cypress filter, you can run specific tryout that meet multiple weather. This can be very useful when you have a large test suite and motive to run only a specific subset of tryout.

Best practices for utilise Cypress tags

Here are some best practices for using Cypress tatter:

  1. Use meaningful tag:Use meaningful tag that describe the purpose of the trial or the character of test. Avoid using generic tags like & # 8220; smoke & # 8221; or & # 8220; fixation & # 8221; and rather use rag that provide more context, such as & # 8220; login, & # 8221; & # 8220; checkout, & # 8221; & # 8220; execution, & # 8221; etc.
  2. Limit the number of tags: Limit the act of rag used for each test to keep things simple and avoid clutter. Try to use only a few relevant tags for each test.
  3.  Avoid parallel tags:Avoid duplicating tags for alike tests. Instead, use a single tag that describes the group of tests.
  4. Consistency in tagging:Ensure consistency in tagging tryout, using the like tag name for trial that have a similar purpose.
  5. Use tag to grouping exam: Use shred to group tests and run specific tests based on tags. This can be very useful when you have a orotund tryout suite and need to run entirely a specific subset of test.
  6. Keep tags up-to-date:Keep tags up-to-date as the test suite evolves, impart and removing tatter as needed to ensure that tests are properly grouped and that the tags continue to be meaningful.

By following these best practices, you can effectively use Cypress tags to organize and care your test cortege. This can help you to run specific exam, except specific tests from execution, and improve the overall efficiency of your test suite.

Use of Cypress tags with BrowserStack Cypress plugin

The BrowserStack Cypress plugin allows you to run your Cypress trial on the BrowserStack cloud infrastructure, cater you with admission to a encompassing range of browsers and devices. If you are using the BrowserStack Cypress plugin, you can nonetheless use Cypress ticket to organize and categorize your tests. Here are the steps to install and run cypress tags with the BrowserStack plugin.

1. 

2. Once you have installed and set up the Browserstack Cypress plugin, you can add tags to your Cypress tests as usual. Here & # 8217; s an example

describe ('Login page ', () = & gt; {

it ('should display error message on invalid login ', () = & gt; {

// test logic

}, {shred: ['login ', 'error ']});

});

In this example, the examination has two tags: & # 8220; login & # 8221; and & # 8220; error. & # 8221; You can add as many tags as you need to each test.

3. To run your tests with specific tags, you can use the Cypress CLI as usual. Here & # 8217; s an example:

cypress run -- spec cypress/integration/login.spec.js -- tag login

In this example, let us run the login.spec.js test file and run tests only with the & # 8220; login & # 8221; tag. The Browserstack Cypress plugin will run the tests on the BrowserStack cloud infrastructure, provide you with access to a wide range of browser and device.

Using Cypress tags with the Browserstack Cypress plugin is a simple and effective way to organize and categorize your tests, making it easier to contend large test suites and run specific tests as needed. By follow these stairs, you can easily add tags to your Cypress tests and run them on the BrowserStack cloud substructure, providing you with approach to a panoptic range of browser and devices for comprehensive testing.

Talk to an Expert

Troubleshooting tag filtering errors

Here are some common issues and solutions when troubleshooting tag filtering errors in Cypress:

  1. Incorrect tag syntax: Make sure that the tag syntax is right, using the tag and syntax in the comment to add tags to tests. If the tag syntax is incorrect, tests may not be properly tagged, and filtering may not work as ask.
  2. 2. Inconsistent tag names: Make sure that tag names are consistent, using the same tag name for tests that have a alike role. Inconsistent tag names can confuse and lead to incorrect filtering results.
  3. Misspelled tag names:Make sure that tag names are write right, as misspelled tag name can get errors when trickle exam.
  4. Missing tags:Make sure that all relevant tests experience been label. If tryout are not tagged, they will not be included when filtering by tag.
  5. Outdated tags:Make sure that tags are up-to-date, adding and remove ticket as needed to control that tests are decent grouped and that the ticket continue to be meaningful.

In conclusion, Cypress rag are a powerful tool for organizing and managing your. By impart tags to tests, you can group tests, run specific tests based on ticket, and except specific tests from performance. When habituate correctly, Cypress tags can help to amend the efficiency and reliability of your test suite.

Read More:

However, it is important to follow best practices for using Cypress ticket, such as using meaningful tags, limiting the number of tags, debar duplicating rag, and keeping tags up-to-date. By following these good practices, you can effectively use Cypress tatter to organize and manage your test suite.

If you encounter any issues when filtering tests by tag in Cypress, there are troubleshooting tips available to facilitate you resolve most problems. By following these tips, you can ensure that your trial suite is decent organized and handle and that you are able to efficaciously trickle tests based on tag.

Overall, Cypress tags are a valuable tool for any test suite, providing a convenient and flexile way to direct and handle tests. Whether you are just starting with Cypress or have been using it for a while, incorporating rag into your prove workflow can help to streamline and improve the overall efficiency of your testing process.

Running Cypress tests on BrowserStack can aid you improve the calibre, reliability, and scalability of your testing efforts, which can ultimately take to a best user experience for your customers.

  • BrowserStack provides a cloud-based infrastructure for running Cypress examination on multiple browser and operate scheme.
  • It offers a range of examine capabilities, including, meshing throttling, and debug tools to help identify and fix topic in your application.
  • Scaling prove try get easier and more efficient as across multiple browsers and devices.
  • Running Cypress trial on BrowserStack can help meliorate the overall quality and reliability of your application.
  • Ultimately, this can lead to a best user experience for your customers.
Tags
39,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free