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
itinsidedescribecan make tests disorganized. - Using the same description in multiple
itblocks can confuse test reports. - Forgetting to include assertions inside
itleads 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
| Keyword | Purpose | Parameters |
|---|---|---|
| describe | Groups related tests | string description, callback function |
| it | Defines a single test case | string 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.