Open In App

How to skip a test in Cypress conditionally ?

Last Updated : 25 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Cypress, there might be scenarios where you want to skip a test conditionally based on certain criteria, such as browser type, environment variables, or application state. Cypress provides several ways to conditionally skip tests, including using this.skip(), environment variables, or checking specific conditions inside the test. This can be helpful when some tests are not relevant or cannot run in certain contexts (e.g., mobile devices or certain environments).

Example:

Example 1: Skipping a Test Based on a Browser Type

In this example, we will skip a test if the browser is Firefox.

Cypress Test Code

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Browser-Specific Test Example</title>
</head>
<body>
    <h1>Welcome to our site</h1>
    <p>This page is used for testing browser-specific behavior in Cypress.</p>
</body>
</html>
spec.cy.js
describe('Browser-Specific Test', () => {
  it('should skip this test in Firefox', function() {
    // Skip the test if the browser is Firefox
    if (Cypress.browser.name === 'firefox') {
      this.skip();
    }
    
    // The rest of the test for other browsers
    cy.visit('http://localhost:3000');
    cy.get('h1').should('have.text', 'Welcome to our site');
  });
});

Explanation

  • Cypress.browser.name: Checks the name of the current browser.
  • this.skip(): Skips the test if the condition (browser is Firefox) is met. If not, the test will proceed as usual for other browsers.

Output: If the test is run in Firefox, it will be skipped, and the other tests will continue. In other browsers, the test will run as expected.

output
Output

Example 2: Skipping a Test Based on an Environment Variable

In this example, we will skip a test if an environment variable SKIP_TESTS is set to true.

Cypress Test Code

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Environment-Based Test Example</title>
</head>
<body>
    <h1>Welcome to our site</h1>
    <p>This page is used for testing environment-based behavior in Cypress.</p>
</body>
</html>
spec.cy.js
describe('Environment-Based Test', () => {
  it('should skip this test based on an environment variable', function() {
    // Skip the test if the environment variable SKIP_TESTS is true
    if (Cypress.env('SKIP_TESTS') === true) {
      this.skip();
    }
    
    // Proceed with the test if the condition is not met
    cy.visit('http://localhost:3000');
    cy.get('h1').should('have.text', 'Welcome to our site');
  });
});

Explanation

  • Cypress.env('SKIP_TESTS'): Checks if the SKIP_TESTS environment variable is set to true. This can be configured in your cypress.json or passed as an environment variable during test execution.
  • this.skip(): Skips the test if the environment variable is true.

Running the Cypress Test

To run the test and control whether it skips based on the environment variable, you can set the variable when running Cypress. For example:

  • To skip the test:
npx cypress run --env SKIP_TESTS=true
  • To run the test without skipping:
npx cypress run --env SKIP_TESTS=false

Output

If the environment variable SKIP_TESTS is true, the test will be skipped. If the environment variable is not set or set to false, the test will run normally.

output
Output

Conclusion

Cypress allows you to conditionally skip tests by using this.skip() in combination with different conditions, such as browser type or environment variables. This flexibility ensures that your test suite can adapt to various contexts, making your tests more efficient and applicable only when necessary.


Similar Reads