0
0
CypressHow-ToBeginner ยท 3 min read

How to Use describe and it in Cypress for Testing

In Cypress, use describe to group related tests and it to define individual test cases. describe helps organize tests logically, while it contains the actual test steps and assertions.
๐Ÿ“

Syntax

describe defines a test suite or group of related tests. It takes a string description and a callback function containing one or more it blocks.

it defines a single test case. It also takes a string description and a callback function with the test code and assertions.

javascript
describe('My Test Suite', () => {
  it('does something expected', () => {
    // test steps and assertions here
  })
})
๐Ÿ’ป

Example

This example shows a simple Cypress test using describe to group tests and it to check the page title.

javascript
describe('Google Homepage', () => {
  it('should have the correct title', () => {
    cy.visit('https://www.google.com')
    cy.title().should('include', 'Google')
  })
})
Output
Test passes if the page title includes 'Google'.
โš ๏ธ

Common Pitfalls

  • Not nesting it inside describe can make tests disorganized.
  • Using the same description in multiple it blocks can confuse test reports.
  • Forgetting to include assertions inside it leads to tests that do not verify anything.
javascript
/* Wrong way: <code>it</code> outside <code>describe</code> */
it('test without describe', () => {
  cy.visit('https://example.com')
})

/* Right way: <code>it</code> inside <code>describe</code> */
describe('Example Site', () => {
  it('loads the homepage', () => {
    cy.visit('https://example.com')
  })
})
๐Ÿ“Š

Quick Reference

KeywordPurposeParameters
describeGroups related testsstring description, callback function
itDefines a single test casestring description, callback function
โœ…

Key Takeaways

Use describe to group related tests logically.
Write each test case inside an it block with clear descriptions.
Always include assertions inside it to verify behavior.
Keep test descriptions unique to avoid confusion in reports.
Nest it blocks inside describe for better organization.