Before and after hooks help run setup and cleanup steps automatically in tests. They make tests easier and cleaner by avoiding repeated code.
You want to open a website before each test starts.
You need to reset data after a test finishes.
You want to log in once before running several tests.
You want to clear cookies or local storage after tests.
You want to prepare test data before running tests.
Syntax
Cypress
before(() => {
// code to run once before all tests
});
after(() => {
// code to run once after all tests
});
beforeEach(() => {
// code to run before each test
});
afterEach(() => {
// code to run after each test
});
before and after run once per test suite.
beforeEach and afterEach run before or after every single test.
This test suite shows how hooks run in order. Logs help see when each hook and test runs.
Cypress
describe('My Test Suite', () => {
before(() => {
cy.log('Runs once before all tests')
})
after(() => {
cy.log('Runs once after all tests')
})
beforeEach(() => {
cy.log('Runs before each test')
})
afterEach(() => {
cy.log('Runs after each test')
})
it('Test 1', () => {
cy.log('Executing Test 1')
})
it('Test 2', () => {
cy.log('Executing Test 2')
})
})
OutputSuccess
Important Notes
Hooks help keep tests DRY (Don't Repeat Yourself).
Use beforeEach and afterEach for setup/cleanup needed for every test.
Use before and after for one-time setup or cleanup.
Summary
Before and after hooks run code automatically around tests.
Use before/after once per suite, beforeEach/afterEach per test.
They make tests cleaner and easier to maintain.
Practice
(1/5)
1. What is the main purpose of before and after hooks in Cypress testing?
easy
A. To define test data inside tests
B. To write the main test steps
C. To skip tests conditionally
D. To run setup and cleanup code once per test suite
Solution
Step 1: Understand the role of hooks
Hooks like before and after run code automatically before or after tests.
Step 2: Identify their scope
before and after run once per test suite, ideal for setup and cleanup.
Final Answer:
To run setup and cleanup code once per test suite -> Option D
Quick Check:
Hooks run code around tests = D [OK]
Hint: Remember: before/after run once per suite, beforeEach/afterEach per test [OK]
Common Mistakes:
Confusing before with beforeEach
Thinking hooks run inside tests
Using hooks to skip tests
2. Which of the following is the correct syntax to run a function once before all tests in a Cypress test suite?
easy
A. before(() => { cy.visit('/') })
B. beforeEach(() => { cy.visit('/') })
C. after(() => { cy.visit('/') })
D. afterEach(() => { cy.visit('/') })
Solution
Step 1: Identify the hook that runs once before all tests
before runs once before the entire suite; beforeEach runs before each test.
Step 2: Match the syntax
before(() => { cy.visit('/') }) is the correct syntax to run once before all tests.
Final Answer:
before(() => { cy.visit('/') }) -> Option A
Quick Check:
Run once before all tests = before() = A [OK]
Hint: Use before() for once before all tests, beforeEach() for every test [OK]
Common Mistakes:
Using beforeEach instead of before for once-per-suite setup
Confusing after with before
Incorrect arrow function syntax
3. Consider this Cypress code snippet:
describe('My Suite', () => {
before(() => {
cy.log('Setup once')
})
beforeEach(() => {
cy.log('Setup before each test')
})
it('Test 1', () => {
cy.log('Running Test 1')
})
it('Test 2', () => {
cy.log('Running Test 2')
})
})
What is the order of logs printed during test execution?
medium
A. Setup before each test, Setup once, Running Test 1, Running Test 2
B. Setup once, Setup before each test, Running Test 1, Setup before each test, Running Test 2
C. Running Test 1, Setup once, Setup before each test, Running Test 2
D. Setup once, Running Test 1, Setup before each test, Running Test 2
Solution
Step 1: Understand hook execution order
before runs once before all tests, beforeEach runs before each test.
Step 2: Trace the logs
Logs print in this order: 'Setup once' (before all), then for Test 1: 'Setup before each test', 'Running Test 1', then for Test 2: 'Setup before each test', 'Running Test 2'.
Final Answer:
Setup once, Setup before each test, Running Test 1, Setup before each test, Running Test 2 -> Option B
Quick Check:
before runs once, beforeEach runs before each test = A [OK]
Hint: Remember: before runs once, beforeEach runs before every test [OK]
But the cy.clearCookies() is not running after each test as expected. What is the problem?
medium
A. after runs once after all tests, not after each test
B. You must call cy.clearCookies() inside each test manually
C. after should be afterEach to run after each test
D. beforeEach should be before to clear cookies
Solution
Step 1: Understand the difference between after and afterEach
after runs once after all tests; afterEach runs after each test.
Step 2: Identify the problem
after runs once after all tests, not after each test.
Final Answer:
after runs once after all tests, not after each test -> Option A
Quick Check:
after runs once per suite = A [OK]
Hint: Use afterEach for per-test cleanup, after for once after all tests [OK]
Common Mistakes:
Using after instead of afterEach for per-test cleanup
Expecting after to run multiple times
Manually clearing cookies inside tests
5. You want to run a setup step once before all tests, a cleanup step once after all tests, and also run a login step before each test in Cypress. Which code snippet correctly implements this?