Describe Blocks and Hooks

Group tests well and reuse setup with hooks.

Playwright Automation Module 3 Lesson 1
9 min read

What you'll learn

  • Group related tests with describe.
  • Use beforeEach for shared setup.
  • Use afterEach for cleanup.

Describe Blocks and Hooks

Group tests well and reuse setup with hooks.

Big Picture

One small picture can make this idea easier to hold.

Well grouped

  • Related tests stay together.
  • Setup lives in one place.
  • The file is easier to scan.
  • Failures are easier to read.

Scattered

  • Same setup gets copied.
  • Tests are hard to follow.
  • Cleanup gets missed.
  • The file grows messy.

VerdictGroups and hooks keep test files tidy.

How It Moves

Short steps make the flow easier to see.

1

Group the goal

Put similar tests in one describe block.

2

Add setup

Use beforeEach for common prep.

3

Run tests

Each test starts from the same base state.

4

Clean up

Use afterEach if you need to reset things.

Step By Step

This is the same idea, stretched across time.

  1. 1

    Write the group

    Name the related tests with describe.

  2. 2

    Add shared setup

    Use a hook for repeated prep.

  3. 3

    Run each test

    Let the hook handle the base state.

  4. 4

    Reset if needed

    Clean up after the test ends.

One Small Model

Think of this like a tiny card you can keep in your pocket.

Test tree

5 fields
Describe:"Checkout flow"
beforeEach:"Open page"
Test 1:"Happy path"
Test 2:"Error path"
afterEach:"Clean state"
Hooks sit around the tests like setup and cleanup rails.

Quick Check

Question 1 of 10 correct

What is beforeEach for?

Map It

One more picture helps you see where this lesson matters most.

Structure map

File clarity
Easy to read

Best

One topic per describe block

Good

Shared setup in beforeEach

Weak

Copy the same prep everywhere

Okay

Small file with no hooks

Hard to read
LooseTight
Test grouping
Good structure makes tests easy to scan and update.

Final Quiz

Question 1 of 30 correct

What is describe used for?