Verify skipping and focusing tests using .skip and .only in Cypress
Preconditions (2)
✅ Expected Result: Only the third test case runs and passes; the second test case is skipped; the first test case does not run
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.
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.
.skip method do in Cypress testing?.skip.skip method is used to tell Cypress to ignore a test or group during execution..only, which runs only the marked tests, .skip prevents the marked tests from running..skip = skip test [OK]it.only syntax runs only the marked test and skips all others.it.skip skips a test, describe.only runs only a group, and describe.skip skips a group.it.only runs only one test [OK]describe('Group A', () => {
it('Test 1', () => { /* code */ })
it.only('Test 2', () => { /* code */ })
it('Test 3', () => { /* code */ })
})
describe.only('Group B', () => {
it('Test 4', () => { /* code */ })
it('Test 5', () => { /* code */ })
})describe.only effectdescribe.only('Group B') means only Group B runs; Group A is skipped.it.only inside Group Ait.only('Test 2') is inside Group A, Group A is skipped entirely because of describe.only on Group B.describe.only overrides it.only [OK]describe.skip('Group X', () => {
it.only('Test A', () => { /* code */ })
it('Test B', () => { /* code */ })
})
describe('Group Y', () => {
it('Test C', () => { /* code */ })
})
What is the problem with this code?describe.skip effect on Group Xit.only.describe.skip skips whole group, ignoring it.only [OK]describe('My Tests', () => {
it('Test 1', () => { /* code */ })
it('Test 2', () => { /* code */ })
it('Test 3', () => { /* code */ })
})
Options:
A) Add .only to Test 2 and .skip to Test 3.
B) Add .skip to Test 1 and .only to Test 3.
C) Add .only to Test 1 and .skip to Test 2.
D) Add .skip to Test 2 and .skip to Test 3..only on a test runs only that test, skipping all others unless they also have .only. Using .skip skips that test..only to Test 2 and .skip to Test 3. Test 1 will be skipped automatically because of .only..only runs test, .skip skips test [OK]Pass the test and this pattern moves unknown