Imagine you run automated tests that create users in a system. Why should you clean up this test data after the tests finish?
Think about how leftover data might affect later tests.
Cleaning test data prevents leftover entries from interfering with future tests, ensuring tests run reliably and results stay accurate.
Consider this Cypress command that deletes a user by ID after a test:
afterEach(() => {
cy.request('DELETE', '/api/users/123')
.then((response) => {
cy.log(response.status)
})
})What will be logged if the user is successfully deleted?
afterEach(() => {
cy.request('DELETE', '/api/users/123')
.then((response) => {
cy.log(response.status)
})
})HTTP 200 means success for DELETE requests.
If the user exists and is deleted, the server responds with status 200, which is logged by cy.log.
You want to check that a user with ID 456 no longer exists after cleanup. Which Cypress assertion is correct?
cy.request({ method: 'GET', url: '/api/users/456', failOnStatusCode: false })
.its('status')
.should(/* fill here */)Think about the HTTP status code when a resource is not found.
A 404 status means the user was not found, confirming cleanup was successful.
Look at this Cypress cleanup code snippet:
after(() => {
return cy.request('DELETE', '/api/users/789')
})Tests pass but data is not deleted. What is the likely cause?
Consider how Cypress handles asynchronous commands in hooks.
after() runs once after all tests, but without returning or chaining the request, Cypress may finish before the request completes, so deletion might not happen.
You have many tests creating users. You want to clean all test users after the test suite finishes. Which approach is best?
Think about efficiency and reliability for large test suites.
Using an after() hook with a single API call to delete all test users by a tag is efficient and ensures cleanup happens once after all tests.