0
0
Testing Fundamentalstesting~8 mins

Contract testing basics in Testing Fundamentals - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Contract testing basics
Folder Structure
contract-testing-project/
├── contracts/           # Contract definitions (e.g., JSON schemas, Pact files)
│   ├── consumer-provider.json
│   └── ...
├── tests/               # Contract test implementations
│   ├── consumer/
│   │   └── consumerContractTest.js
│   └── provider/
│       └── providerContractTest.js
├── utils/               # Helper functions for contract tests
│   └── contractUtils.js
├── config/              # Environment and test configuration
│   └── config.js
├── reports/             # Test reports and logs
│   └── contractTestReport.html
├── package.json         # Project dependencies and scripts
└── README.md            # Project overview and instructions
Test Framework Layers
  • Contract Definitions Layer: Stores the contract files that describe the expected API interactions between consumer and provider.
  • Test Implementation Layer: Contains tests that verify the contracts from both consumer and provider sides.
  • Utilities Layer: Helper functions to load contracts, mock data, or validate responses.
  • Configuration Layer: Manages environment variables, endpoints, and test settings.
  • Reporting Layer: Generates readable reports showing contract test results.
Configuration Patterns
  • Environment Variables: Use environment variables to switch between test environments (e.g., dev, staging).
  • Contract Versioning: Keep contract files versioned to track changes and compatibility.
  • Credentials Management: Store sensitive data like API keys securely, using environment variables or secret managers.
  • Test Settings: Configure timeouts, retries, and mock server URLs in a central config file.
Test Reporting and CI/CD Integration
  • Generate clear reports showing which contracts passed or failed.
  • Integrate contract tests into CI pipelines to run on every code push.
  • Fail builds if contract tests fail to prevent breaking changes.
  • Use badges or dashboards to show contract test status to the team.
Best Practices
  • Keep Contracts Simple and Clear: Contracts should be easy to understand and maintain.
  • Test Both Consumer and Provider: Verify contracts from both sides to ensure agreement.
  • Automate Contract Verification: Run contract tests automatically in CI to catch issues early.
  • Version Contracts Carefully: Manage contract changes to avoid breaking consumers or providers.
  • Use Mocks for Consumer Tests: Mock provider responses to test consumer behavior without needing the real provider.
Self Check

Where in this folder structure would you add a new contract file for a new API endpoint between a consumer and provider?

Key Result
Contract testing frameworks organize contract files, tests, utilities, configs, and reports to verify API agreements between services.