Data cleanup helps keep tests independent and reliable by removing leftover data after tests run.
Data cleanup approaches in Cypress
Introduction
Syntax
Cypress
after(() => {
// cleanup code here
cy.request('DELETE', '/api/test-data')
})after() runs once after all tests in a block finish.
You can also use afterEach() to clean after each test.
Examples
Cypress
after(() => {
cy.request('DELETE', '/api/users/testuser')
})Cypress
afterEach(() => {
cy.request('POST', '/api/cleanup')
})Cypress
before(() => {
cy.request('POST', '/api/reset-database')
})Sample Program
This test suite resets the database before tests, creates a user during the test, and deletes that user after all tests finish.
Cypress
describe('User tests with cleanup', () => { before(() => { cy.request('POST', '/api/reset-database') }) it('creates a new user', () => { cy.request('POST', '/api/users', { username: 'testuser' }) .then(response => { expect(response.status).to.eq(201) }) }) after(() => { cy.request('DELETE', '/api/users/testuser') }) })
Important Notes
Always clean data to avoid tests affecting each other.
Use API calls or database commands for faster cleanup than UI actions.
Be careful to only delete test data, not real data.
Summary
Data cleanup keeps tests independent and reliable.
Use before, after, beforeEach, or afterEach hooks for cleanup.
API requests are efficient for cleaning test data.
Practice
1. Why is data cleanup important in Cypress tests?
easy
Solution
Step 1: Understand test independence
Tests should not depend on data left by previous tests to avoid false results.Step 2: Recognize reliability benefits
Cleaning data ensures each test starts fresh, making results trustworthy.Final Answer:
It keeps tests independent and reliable -> Option CQuick Check:
Data cleanup = test independence and reliability [OK]
Hint: Data cleanup ensures fresh start for each test [OK]
Common Mistakes:
- Thinking cleanup slows tests
- Believing cleanup causes more failures
- Assuming cleanup removes assertions
2. Which Cypress hook is best to use for cleaning test data after each test?
easy
Solution
Step 1: Identify hooks for cleanup timing
afterEachruns after every test, ideal for cleanup after each test.Step 2: Compare other hooks
beforeandbeforeEachrun before tests;afterruns once after all tests.Final Answer:
afterEach-> Option AQuick Check:
Cleanup after each test =afterEach[OK]
Hint: Use
afterEach to clean after every test [OK]Common Mistakes:
- Using
beforefor cleanup - Confusing
beforeEachwith cleanup hook - Using
afterwhich runs only once
3. What will happen if you use this code snippet in Cypress?
after(() => {
cy.request('DELETE', '/api/test-data')
})medium
Solution
Step 1: Understand
afterhook behaviorafterruns once after all tests in the suite complete.Step 2: Analyze the API request timing
The DELETE request runs only once after all tests, cleaning data once at the end.Final Answer:
Test data is cleaned once after all tests finish -> Option BQuick Check:
after= cleanup once after all tests [OK]
Hint:
after runs once after all tests finish [OK]Common Mistakes:
- Thinking
afterruns after each test - Confusing
afterwithafterEach - Assuming cleanup runs before tests
4. You wrote this code to clean test data after each test:
But test data is not cleaned properly. What is the likely problem?
afterEach(() => {
cy.request('DELETE', '/api/test-data')
})But test data is not cleaned properly. What is the likely problem?
medium
Solution
Step 1: Verify
afterEachhook validityafterEachis valid and commonly used for cleanup in Cypress.Step 2: Check API request issues
If data is not cleaned, the DELETE request may fail due to wrong URL or server issues.Final Answer:
The API endpoint URL might be incorrect or unavailable -> Option AQuick Check:
Failed cleanup often means API endpoint problem [OK]
Hint: Check API URL if cleanup fails [OK]
Common Mistakes:
- Thinking
afterEachis unsupported - Using wrong HTTP method assumptions
- Switching hooks unnecessarily
5. You want to ensure test data is cleaned efficiently before and after each test in Cypress. Which approach is best?
hard
Solution
Step 1: Understand test data lifecycle
Creating data before each test and cleaning after each test keeps tests isolated and reliable.Step 2: Use API requests in hooks
API calls inbeforeEachandafterEachefficiently manage test data per test.Final Answer:
UsebeforeEachto create data andafterEachto delete data via API requests -> Option DQuick Check:
Setup and cleanup per test =beforeEach+afterEach[OK]
Hint: Use
beforeEach and afterEach with API calls [OK]Common Mistakes:
- Cleaning data only once before or after all tests
- Not using hooks for cleanup
- Manually deleting data inside tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
