How to run specific test in Cypress

Related Product On This Page Why do specific tests in Cypress?June 14, 2026 · 12 min read · Tool Comparison

Related Product

How to run specific test in Cypress

Running specific tests in Cypress is important for effective debugging and quick feedback during maturation. Cypress let you to isolate and run only the or test files you ’ re focused on, Instead of executing the integral test suite, saving time and resource.

In this guide, learn how to run specific exam in Cypress along with the best practices.

Why do specific tests in Cypress?

Running specific tests in Cypress is significant for speeding up evolution workflows, isolating issue, and increase test efficiency. When working on turgid test suites, re-running the entire suite for every small code change can be time-consuming and unnecessary.

By targeting case-by-case specs, developer can quickly formalise functionality, debug failure, and iterate on test code without waiting for unrelated examination to complete. This helps to improve productivity and provides faster feedback during ontogenesis and CI runs.

Key Benefits:

  • Faster Iteration:Quickly exam only the relevant portion of your application.
  • Easy Debugging:Debug betray tests for easier troubleshooting.
  • Targeted Validation:Run examination related to recent changes or specific features.
  • Time Efficiency:Avoid lam the total retinue when unnecessary.

Read More:

Command to Run Specific Test File in Cypress

To run a specific trial file in Cypress, use the& # 8211; spec flagwith thenpx cypress run command.

This narrate Cypress to execute only the specified test file, rather than running the entire test suite. Here ’ s the canonic syntax:

npx cypress run -- spec `` cypress/e2e/ & lt; file-name & gt; .cy.js ''

Example:

npx cypress run -- spec `` cypress/e2e/login.cy.js ''

This command runs only the login.cy.js test file. A user can also provide a relative or absolute path to the spec file.

For targeting multiple file, use global patterns like:

npx cypress run -- spec `` cypress/e2e/auth/ *.cy.js"

Read More:

Beginning with Cypress

Let & # 8217; s search how to use Cypress to test a web coating now that we are relatable with what it is and its advantages. Before beginning, make sure you clone the GitHub seed.

Adding Cypress to our project is step one. The rattling initiative motility you must do is access the projection leaflet you just cloned in the pole and run the following command to install all of its dependencies:

npm install

After the command has finished running, we & # 8217; ll need to add Cypress to it by using the command that goes like this:

npm install cypress -- save-dev

Go to the labor & # 8217; s rootage and chance thepackage.jsonfile. Inside the & # 8220; playscript, & # 8221; add the chase:

`` cypress: exposed '': `` cypress open '', '' cypress: run '': `` cypress run ''

The final measure is to launch Cypress directly from the npm command:

npm run test

A local instance of the website should be opened by your browser, along with a Cypress window, which you may now close. The local server might be cease as well.

If a server is already running, Cypress just has to be depart using npx Cypress outdoors. However, you may use npm run test to get the default server.

  • cypress run specific test:
cypress run -- spec 'path/to/files/ * .spec.js '
  • cypress run specific test:
cypress run -- spec path/to/file.spec.js

Learn how you can Run Cypress Tests with this

Launch your first BrowserStack Cypress test using Command Line

Get the BrowserStack CLI instal. A command-line program called the BrowserStack & # 8211; Cypress CLI is used to facilitate communication between Cypress and BrowserStack. Install the BrowserStack & # 8211; Cypress CLI as follow using the npm bidding:

# Install the BrowserStack Cypress CLI npm install -g browserstack-cypress-cli

Check out the Official Documentation to memorize how to

Creating a test in Cypress

Let & # 8217; s write our inaugural test now that Cypress has been introduced to our project. Navigate to/cypress/integration and add a mycart_spec.jsfile to it.

We & # 8217; ll insert the following code into this file:

describe (`` Test Ecommerce features '', () = & gt; {it (`` Test Ecommerce features '', () = & gt; {cy.viewport (1366, 900); cy.visit (`` localhost:3000 ''); waitForSnipcartReady (); cy.get (`` # la-gioconda '') .click (); cy.get (`` .total '') .should (`` contain '', `` 79.99 '', {timeout: 30000}); expect (cy.get (`` .mycart-modal '') .should (`` exist ''));});

With this, a 1366 & # 215; 900 viewport will be employ to access our local page. Then, by catch themycart.readycase, we establish a hope to wait for themycartbook to cease loading.

Under the globularcyobject you can see above, all of Cypress & # 8217; s functionalities are accessible. No async/await is required. With the exception of the mycart case, which must be awaited since we need to capture the run document for this, tests run one measure at a time, expect for the completion of the preceding one.

Let & # 8217; s run our initial test now that it has been make.

npm run test

The trial we construct ought to be seeable in the Cypress window. Simply click it to get it running. This will launch a tonic window of the browser and launching the test mechanically.

Testing a Single-Page Application

We & # 8217; ll test a single-page application (SPA) in this presentment that make use of the React frontend framework. Users may add, edit, and remove tasks using the coating & # 8217; s simple task manager.

Installing sure dependencies is the first pace. The following are necessary:

  • React
  • Create React App
  • Cypress

With npm, we can set up these dependencies:

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

npm install -- relieve react react-dom create-react-app cypress

We can now useCreate React App:to create a new React app.

create-react-app my-app

The undermentioned additional Cypress dependencies must be installed next:

cd my-app npm install -- save-dev cypress

We can now publish our tests after install our dependencies. In thecypress/integrationbrochure, create a new file namedtasks.spec.jsand occupy it with the following content:

describe ('Tasks ', use () {beforeEach (use () {cy.visit ('http: //localhost:3000 ');}); it ('should add a task ', mapping () {// Enter task details into form and submit cy.get ('input [name= '' taskName ''] ') .type ('My Task '); cy.get ('textarea [name= '' taskDescription ''] ') .type ('My Description '); cy.get ('form ') .submit (); expect (cy.get ('.tasks li ') .its ('length ')) .to.equal (1);});});

Before each test in the specification, the beforeEach function executes. In this instance, we are navigating to the main application URL, which ishttp: //localhost:3000.

The ‘ it ’ function holds the location of our actual test code. A callback function containing our test code is the second arguing, and the first argument is a description of the examination scenario. This exam verifies that when tasks are subject via the form, our task management application appropriately lend them to the leaning. To do this, we first enter some information into the form fields, and so we submit it. There ’ s now one job in the list employ anticipate.

Using npm, you may execute tests from a terminal window.

npm run e2e npm run e2e: open

Reliable results need pass all tests on existent browsers. With BrowserStack, you can start testing the 30+ almost recent browser versions on Windows and Mac. To reach quicker results without compromising caliber, use instant, hassle-free parallelization. By using BrowserStack to test the software under, bugs can be ground before users do.

How to Specify Specific Test Files employ Cypress CLI?

Running specific test file using Cypress CLI is an efficient way to target only the relevant exam, specially when debugging or working on a focussed feature. Cypress furnish the& # 8211; spec flagto control which test files are execute.

Step 1: Open your depot and check for the task radical directory.

Step 2: Use the & # 8211; spec masthead with npx cypress run to orient to the exact test file for running:

npx cypress run -- spec `` cypress/e2e/login.cy.js ''

Step 3: Use glob patterns for running multiple files that tally a certain structure:

npx cypress run -- spec `` cypress/e2e/auth/ * .cy.js ''

Step 4: Target multiple specific files by severalize them with comma:

npx cypress run -- spec `` cypress/e2e/login.cy.js, cypress/e2e/register.cy.js ''

Step 5: Combine with other options like delineate browser or head manner:

npx cypress run -- spec `` cypress/e2e/login.cy.js '' -- lead -- browser chrome

Read More:

Like what you are reading?

You can start discourse with our discord community

How to execute Cypress Tests with Cypress Test Runner?

The helps to run, debug, and inspect your tests interactively in a existent browser. It ’ s specially useful during development for unrecorded reloading, visual debugging, and audit test output in existent time.

Steps to Run Tests Using the Cypress Test Runner

Here are the steps to be postdate for running tests utilise the Cypress Test Runner:

Step 1: In your terminal, run:

npx cypress open

This launches the Cypress Test Runner UI.

Step 2: In Cypress 10+, select a testing case from:

Here, you can select E2E Testing and continue.

Step 3: Select the browser to run the tests (e.g. Chrome, Firefox, Edge).

Step 4: Pick the test file from the lean shown in the UI. Click on any .cy.js or .cy.ts file to run it.

Step 5: The examination will open in an literal browser window where you can:

  • Inspect each bid and assertion
  • View the covering province at each step
  • Re-run or debug failed tests interactively

Step 6: Any modification made to the test code will mechanically recharge the test in the smuggler.

this.skip () for Running Specific Tests in Cypress

In Cypress, this.skip () is an efficient way to skip exam within abefore, beforeEach, or any test hook.

It allows you to dynamically decide whether a test or a group of tests should run based on runtime conditions, such as environment variables, lineament flags, or API answer. This is useful when certain exam are only relevant in specific environs or when a test suit should be temporarily bypassed without commenting it out.

Example:

describe ('Admin Panel Tests ', function () {before (role () {if (Cypress.env ('userRole ')! == 'admin ') {this.skip ();}}); it ('should expose the admin splashboard ', () = & gt; {cy.visit ('/admin '); cy.contains ('Welcome, Admin ') .should ('be.visible ');});});

Use Case: Conditionally Skip a Single Test

In this use case, the test runs only in the staging environment and is skipped in all others using a runtime condition.

it ('should solely run in staging ', function () {if (Cypress.env ('ENV ')! == 'staging ') {this.skip ();} cy.visit ('/beta-feature '); cy.contains ('Staging Feature ') .should ('exist ');});

Read More:

Visual Studio Code Extension for Running Specific Tests in Cypress

To increase the productiveness of Cypress screen workflows, Visual Studio Code (VS Code) ply several extensions that allow running specific tryout or trial file forthwith from the editor, without switching to the depot. One of the virtually popular and effective extension for this purpose is Cypress Snippets in combination with Cypress Test Runner or Cypress Helper.

These extensions furnish features like inline trial running, code lense activeness above trial blocks, and simple navigation to test files.

Democratic VS Code Extensions for Cypress

Here are some of the democratic VS Code extensions expend for Cypress:

1. Cypress Test Runner for Visual Studio Code

  • Add & # 8220; Run & # 8221; and & # 8220; Debug & # 8221; links above each describe () and it () block.
  • Right-click any examination and choose & # 8220; Run Test & # 8221; or & # 8220; Debug Test & # 8221;.
  • Automatically run the exam in the configured Cypress environment.

Example:

describe ('Login Page ', () = & gt; {it ('should log in successfully ', () = & gt; {cy.visit ('/login '); cy.get ('input [name= '' username ''] ') .type ('user '); cy.get ('input [name= '' parole ''] ') .type ('pass '); cy.get ('.login-button ') .click (); cy.url () .should ('include ', '/dashboard ');});});

2. Cypress Snippets

They provide useful auto-completions and easy-to-modify test templates. While they don ’ t run exam directly, they speeds up test penning.

3. Code Runner

If configured correctly, with a codification moon-curser, a exploiter can map custom Cypress run hand to VS Code project.

Read More:

Best Practices to run Specific Tests in Cypress

Cypress is a fantastic end-to-end testing creature that too has top-notchBetter Practices documentation. Here is a second I & # 8217; ve written that proceed into more point on Best practices.

In essence:

  • Don & # 8217; t utilise the UI to establish up state; alternatively, set the province programmatically.
  • Write specifications singly and prevent coupling.
  • Try not to restrict yourself by acting like a user.
  • Always be able to run tests independently and have them walk.
  • Test just what you can control.
  • To give your circumstance, usedata-* attributes.
  • Clean up province before tests run (not after).

Pro-Tip:Integrate your Test Suite with. On a motley of literal mobile and pad devices, such as the iPhone, iPad, Samsung Galaxy, Google Pixel, Nexus, and others, test your aboriginal and hybrid apps. For your disunited online and mobile audience, create unbelievable application.

Why Choose BrowserStack for Cypress Testing

BrowserStack is a cloud-based testing program that lets you run Cypress test efficiently across multiple device-OS-browser combinations. Here are a few reasons why you should choose BrowserStack:

  • : Cypress runs on limited browser, mainly Chrome-based one. BrowserStack helps you expand your Cypress tests to many other browsers, such as Safari, Edge, IE, and more.
  • Cloud Infrastructure: This cloud-based platform make not require fix up or conserve browser or physical devices topically.
  • Parallel Testing: With, you can run multiple Cypress tests concurrently, hasten up test execution and finally the release cycles.
  • Real-device testing: BrowserStack offers you a vast, letting you run Cypress examination on 3500+ existent device, browser, and OS combinations, thence allow you to prove under.
  • Integrations: BrowserStack offers seamless s with several like, Circle CI, Bamboo and more.
  • Scalability: BrowserStack endorse real-device and parallel testing on a cloud-based infrastructure, enabling you to run 100 of Cypress tests across different environments.

Talk to an Expert

Conclusion

Without the hassles; Cypress can be set up in a matter of minutes. Cypress & # 8217; s declamatory user community is e'er eager to offer its expertise and perceptiveness. The syntax used in Cypress tests is pretty easygoing and simple to translate and write. You will be capable to write effective UI tests with a little recitation. For those who wish to develop pertinent end-to-end tests fast and efficiently, Cypress is a outstanding option. It too makes problem-solving very simple.

Utilitarian Resources for Cypress

Understanding Cypress

Use Cases

Tool Comparisons

Tags
36,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