Running a Single Test in Cypress

Running a Single Test in Cypress

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When developing automated tests using Cypress, you might often find yourself wanting to run just one test to quickly verify its behavior without having to wait for other tests to complete. This guide will walk you through various methods to achieve this, ensuring you can streamline your testing process effectively.

Running a Single Test File

If you want to run only one test file, you can use the following command:

cypress run --spec path/to/file.spec.js

You can also use glob patterns to run multiple files matching a specific pattern:

cypress run --spec 'path/to/files/*.spec.js'

Note: Make sure to wrap your glob patterns in single quotes to avoid shell expansion.

Running a Single Test in a File

Cypress provides a convenient way to run a single test within a file by using the .only method. You can apply it to it, describe, or context blocks:

it.only('only run this one', () => {
  // test code here
});

it('not this one', () => {
  // test code here
});

Similarly, you can use describe.only to run all tests within a specific describe block:

describe.only('only this describe block', () => {
  it('test 1', () => {
    // test code here
  });
  it('test 2', () => {
    // test code here
  });
});

Skipping Tests

Additionally, Cypress allows you to skip tests using the .skip method:

it.skip('skip this test', () => {
  // test code here
});

describe.skip('skip this describe block', () => {
  it('test 1', () => {
    // test code here
  });
  it('test 2', () => {
    // test code here
  });
});

Using Visual Studio Code Extension

For those who use Visual Studio Code, there is a handy extension called Test Utils that makes adding and removing .only and .skip easier with keyboard shortcuts. This extension supports JavaScript, CoffeeScript, and TypeScript.

Additional Methods

Another method to run specific tests without modifying the test files is by using the this.skip() function:

it('test page', function () {
  // skip this test for now
  this.skip();
  cy.visit('http://example.com/');
  cy.contains('test page').click();
  cy.url().should('include', '/test-page/');
});

Note: Ensure you use a regular function as the second argument of it, as this will not be available in arrow functions.

Conclusion

By leveraging these methods, you can significantly improve your Cypress testing workflow, allowing you to focus on individual tests without the overhead of running an entire test suite.

For more advanced testing techniques, you might find our Advanced Testing Techniques guide useful.

Efficient Test Automation with Repeato

If you’re looking for a more streamlined way to create, run, and maintain automated tests for your iOS and Android applications, consider using Repeato. Repeato is a no-code test automation tool that utilizes computer vision and AI to provide a fast and intuitive testing experience. It allows you to quickly edit and run tests, and includes both a no-code interface and a scripting interface for more advanced use cases. With support for testing websites inside an Android emulator or device, and explicit web testing support coming soon, Repeato can help you achieve efficient and comprehensive test automation.

Like this article? there’s more where that came from!