MCPContractTestAutomation
This repo is created to demonstrate usage of MCP in the contract testing.
GitHubスター
0
ユーザー評価
未評価
フォーク
0
イシュー
0
閲覧数
2
お気に入り
0
MCP Contract Testing Framework
A robust contract testing framework for REST APIs with Model Context Protocol (MCP) integration, demonstrated using the Petstore Swagger API.
Overview
This framework provides a comprehensive approach to contract testing that leverages the principles of the Model Context Protocol to maintain state, manage context, and validate API responses against OpenAPI/Swagger specifications.
Key Features
- MCP Context Management: Maintains state across test cases, similar to how MCP manages context for AI models
- Automated Contract Generation: Derives contracts from OpenAPI/Swagger specifications
- Schema Validation: Validates API responses against expected schemas
- Contextual Testing: Builds relationships between tests, preserving context for dependent operations
- Test Data Generation: Creates realistic test data based on schema definitions
- Detailed Reporting: Tracks test results with rich metadata for analysis
Project Structure
MCP Contract Testing/
├── config.js # Framework configuration
├── package.json # Project dependencies
├── README.md # This file
└── src/
├── contracts/ # Generated contract files
├── mcp/ # MCP integration modules
│ └── contextManager.js # MCP context management
├── tests/ # Test files
│ ├── pet.test.js # Pet API tests
│ ├── store.test.js # Store API tests
│ └── user.test.js # User API tests
└── utils/ # Utility modules
├── apiClient.js # API client with MCP integration
├── contractGenerator.js # Contract generation utilities
├── contractValidator.js # Schema validation utilities
├── generateContracts.js # Contract generation script
└── testHelper.js # Test utilities
Getting Started
Prerequisites
- Node.js 14+
- NPM or Yarn
Installation
- Clone this repository
- Install dependencies:
npm install
Generating Contracts
Before running tests, generate contract files from the OpenAPI specification:
npm run generate-contracts
This will create JSON contract files in the src/contracts
directory.
Running Tests
Run all tests:
npm test
Run specific test suites:
# Run only pet API tests
npx jest src/tests/pet.test.js
# Run only store API tests
npx jest src/tests/store.test.js
# Run only user API tests
npx jest src/tests/user.test.js
MCP Integration
This framework leverages key concepts from the Model Context Protocol:
Context Management
- Context Creation: Each test suite creates a shared context
- Context Inheritance: Individual tests inherit from shared contexts
- Context Updates: Test results update contexts in real-time
- Context References: Creates relationships between related API operations
Contextual Validation
- Schema Validation: Validates response data against OpenAPI schemas
- Context Consistency: Ensures responses are consistent with established context
- Stateful Testing: Maintains state throughout test execution
Customization
Configuration
Edit config.js
to customize the framework behavior:
- API Settings: Base URL, timeouts, authentication
- MCP Settings: Enable/disable features, configure telemetry
- Validation Settings: Schema validation options
- Test Environment: Parallelization, logging
Adding New Tests
- Create a new test file in
src/tests/
- Import the test helper and context manager
- Create a shared context for the test suite
- Write test cases using the test helper
- Update contexts with test results
Best Practices
- Contextual Testing: Group related API operations in test suites with shared contexts
- Test Isolation: Create new contexts for independent test scenarios
- Context Hierarchies: Use parent-child relationships for contexts when appropriate
- Context References: Create references between related contexts for traceability
- Telemetry: Enable detailed logging for failed tests to aid debugging
Troubleshooting
Known Issues and Solutions
Unknown Format Error:
- Error:
unknown format "int32" ignored in schema
- Solution: Add custom format definitions to Ajv in the ContractValidator constructor.
- Error:
Schema Reference Resolution:
- Error:
can't resolve reference #/definitions/Category from id #
- Solution: Use
SwaggerParser.dereference()
instead ofSwaggerParser.parse()
to properly resolve all references.
- Error:
Open Handles After Tests:
- Issue: Jest detects open handles after tests complete (e.g., "TLSWRAP").
- Solution: Implement a cleanup method that closes connections after tests and call it in the
afterAll
hook.
Schema Validation Failures:
- Issue: External API responses may not strictly conform to the schema.
- Solution: In testing environments, consider making schema validation more lenient or focusing on critical fields only.
Memory Management
Make sure to properly clean up resources after tests complete by:
- Implementing a cleanup method on the TestHelper
- Calling the cleanup method in the afterAll hook of your test files
- Properly closing any open connections
2
フォロワー
23
リポジトリ
0
Gist
0
貢献数