0
0
Cypresstesting~5 mins

describe blocks for grouping in Cypress

Choose your learning style9 modes available
Introduction

We use describe blocks to group related tests together. It helps keep tests organized and easy to understand.

When you want to group tests for a specific feature or page.
When you want to run setup code before all tests in a group.
When you want to clearly separate different test scenarios.
When you want to make test reports easier to read.
When you want to reuse setup or teardown steps for multiple tests.
Syntax
Cypress
describe('group name', () => {
  it('test case 1', () => {
    // test steps
  })

  it('test case 2', () => {
    // test steps
  })
})

The first argument is a string describing the group.

The second argument is a function containing one or more it test cases.

Examples
A simple group with one test case about login.
Cypress
describe('Login feature', () => {
  it('should allow valid user to login', () => {
    // test steps
  })
})
Grouping two related tests about the shopping cart.
Cypress
describe('Shopping Cart', () => {
  it('adds item to cart', () => {
    // test steps
  })

  it('removes item from cart', () => {
    // test steps
  })
})
Sample Program

This test group checks simple calculator operations. Each it block is a test case. The expect assertions check if the results are correct.

Cypress
describe('Calculator', () => {
  it('adds two numbers', () => {
    const result = 2 + 3;
    expect(result).to.equal(5);
  })

  it('subtracts two numbers', () => {
    const result = 5 - 2;
    expect(result).to.equal(3);
  })
})
OutputSuccess
Important Notes

You can nest describe blocks inside each other for more detailed grouping.

Use clear and meaningful names for groups to make reports easy to read.

Summary

describe blocks group related tests together.

They make tests organized and reports clearer.

Each describe contains one or more it test cases.