Test Management
Managing tests efficiently is crucial for maintaining a scalable and well-organized test suite. Cypress provides tools and best practices to organize, configure, and execute tests seamlessly.
Organizing Tests
Keeping your test files and code organized ensures better maintainability and readability.
Folder Structure
When you initialize Cypress, it creates the following folder structure:
cypress/
├── fixtures/ # Static test data (e.g., JSON files)
├── integration/ # Test files (organized by feature or module)
├── plugins/ # Extend Cypress functionality
└── support/ # Custom commands and reusable code
Organizing Tests by Feature
Inside the integration folder, you can create subfolders for each feature or module:
integration/
├── login/
└── login_tests.js
├── dashboard/
└── dashboard_tests.js
Best Practices
- Use meaningful file and folder names.
- Keep individual test files short and focused.
- Group related tests in the same file.
Using Tags to Categorize Tests
Cypress does not natively support test tags, but you can simulate them with custom logic or plugins like cypress-grep.
Example: Using cypress-grep
Install the plugin:
npm install --save-dev cypress-grep
Add the plugin to cypress/plugins/index.js:
module.exports = (on, config) => { require('cypress-grep/src/plugin')(config); return config; };
Use tags in your tests:
describe('Login Tests', { tags: '@critical' }, () => { it('should login successfully', () => { // Test logic }); });
Run tests with a specific tag:
npx cypress run --env grep=@critical
Skipping and Only Running Specific Tests
Run Only One Test
Use .only to run a specific test or suite:
it.only('should test login', () => { // Test logic });
Skip a Test
Use .skip to temporarily ignore a test:
it.skip('should test profile update', () => { // Test logic });
Test Configuration
Each test or suite can have custom settings using the Cypress.config method.
Example: Test-Specific Configuration
it('should load with a custom viewport', () => { Cypress.config('viewportWidth', 1280); Cypress.config('viewportHeight', 720); cy.visit('https://example.com'); });
You can also use test-level overrides:
describe('Custom Configuration', { retries: 2 }, () => { it('should retry on failure', () => { // Test logic }); });
Managing Test Data
Using Fixtures
Fixtures store static test data in JSON files. You can load them in your tests for reusability.
Example: Load Fixture Data
1. Create a file users.json in the fixtures folder:
{ "validUser": { "username": "testUser", "password": "password123" } }
2. Use the fixture in your test:
it('should log in with valid credentials', () => { cy.fixture('users').then((data) => { cy.get('input[name="username"]').type(data.validUser.username); cy.get('input[name="password"]').type(data.validUser.password); cy.get('button[type="submit"]').click(); }); });
Dynamic Test Data
You can generate random test data using libraries like faker.js:
const faker = require('faker'); const randomEmail = faker.internet.email(); cy.get('input[name="email"]').type(randomEmail);
Grouping and Running Tests
Run Tests in Batches
Cypress can run all tests or specific folders/files:
npx cypress run --spec "cypress/integration/login_tests.js"
Parallel Test Execution
Use the Cypress Dashboard to run tests in parallel across multiple machines:
npx cypress run --record --parallel
Test Retries
Sometimes tests fail due to temporary issues like slow responses or flaky elements. Cypress supports automatic retries.
Enable Retries Globally
In cypress.json:
{ "retries": { "runMode": 2, "openMode": 1 } }
Test-Level Retries
it('should retry up to 3 times', { retries: 3 }, () => { // Test logic });
Debugging and Logging
Use Debugging Tools
1. .debug(): Pause the test and inspect the state:
cy.get('button#submit').debug();
2. .pause(): Stop test execution to inspect manually:
cy.pause();
Enable Verbose Logging
Use cy.log() to add custom messages:
cy.log('Navigating to the login page'); cy.visit('/login');
Using Cypress Dashboard
The Cypress Dashboard provides a centralized platform to manage test results, performance, and history. You can:
- View test logs, screenshots, and videos.
- Monitor flaky tests.
- Share test results with your team.
Set up the dashboard by running:
npx cypress run --record --key