0
0
Cypresstesting~15 mins

Skipping and focusing tests (.skip, .only) in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify skipping and focusing tests using .skip and .only in Cypress
Preconditions (2)
Step 1: Write three test cases: one to check the page title, one to check the login form presence, and one to check the login button is disabled initially
Step 2: Mark the second test case with .skip to skip it during test execution
Step 3: Mark the third test case with .only to run only this test case
Step 4: Run the Cypress test suite
Step 5: Observe which tests run and which are skipped
✅ Expected Result: Only the third test case runs and passes; the second test case is skipped; the first test case does not run
Automation Requirements - Cypress
Assertions Needed:
Verify the page title is correct in the first test
Verify the login form exists in the second test
Verify the login button is disabled initially in the third test
Best Practices:
Use .skip and .only correctly to control test execution
Use descriptive test names
Use Cypress commands and assertions consistently
Avoid mixing .skip and .only in the same test suite except for demonstration
Automated Solution
Cypress
describe('Login Page Tests', () => {
  it('checks the page title', () => {
    cy.visit('http://localhost:3000/login');
    cy.title().should('eq', 'Login Page');
  });

  it.skip('checks the login form presence', () => {
    cy.get('form#loginForm').should('exist');
  });

  it.only('checks the login button is disabled initially', () => {
    cy.get('button#loginButton').should('be.disabled');
  });
});

This test suite has three tests for the login page.

The first test visits the login page and checks the title.

The second test is marked with .skip, so Cypress will skip it during the run.

The third test is marked with .only, so Cypress will run only this test and ignore others.

This demonstrates how to focus on or skip tests easily during development or debugging.

Common Mistakes - 3 Pitfalls
Using .only on multiple tests in the same suite
Forgetting to remove .skip or .only before final test runs
Using .skip on important tests without a good reason
Bonus Challenge

Add a test suite with three tests and use .only to run only the second test. Then switch to .skip on the first and third tests.

Show Hint