← Back to libraryQuestion 178 of 468
🌲CypressBeginner

Fixtures and Test Data

📌 Definition:

Fixtures are external files (usually JSON) in cypress/fixtures that hold static test data. cy.fixture loads them so tests use consistent, versioned data and can stub network responses.

📖 Detailed Explanation:

cy.fixture('user.json').then((user) => {...}) loads data for reuse across tests, keeping test bodies clean and data centralized. Fixtures pair naturally with cy.intercept for stubbing: cy.intercept('GET', '/api/user', { fixture: 'user.json' }). This decouples tests from a live backend and makes them deterministic and fast. Fixtures can also be aliased (cy.fixture('x').as('x')) and referenced later. Keep fixtures small and focused; for dynamic data, generate it in the test or via cy.request setup.

🔑 Key Points:
  • Static data in cypress/fixtures (JSON, etc.)
  • cy.fixture loads it; pairs with cy.intercept to stub responses
  • Centralizes and versions test data — deterministic tests
  • Use cy.request/factories for dynamic data instead
🌍 Real-World Example:

A checkout test stubs the cart API with a fixture: cy.intercept('GET', '/api/cart', { fixture: 'cart-3-items.json' }) so the UI always renders the same three items regardless of backend state.

🎯 Scenario-Based Interview Question:

Two tests intermittently interfere because they mutate the same shared fixture object. What's happening and how do you avoid it?