api-testing-verification
Skill1 starsChanged 6 months agoMITView on GitHub
Runs integration tests, contract tests against the OpenAPI spec, and load tests using k6. Validates that the API implementation matches the specification, meets performance requirements, and handles concurrent load. Produces a comprehensive test report. Keywords: testing, integration, contract, load, k6, vitest, coverage, performance, verification, validation, spec-compliance, stress-test, benchmark, regression, e2e
- Installs packages
In this repository12 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- api-authenticationSkill
- api-documentationSkill
- api-performanceSkill
- api-securitySkill
- api-validationSkill
- database-designSkill
- database-migrationsSkill
- deployment-config-generatorSkill
- route-generationSkill
- schema-intakeSkill
- tdd-from-schemaSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 6 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 25
Before deployment or after significant changes.
- Distinctive: line 39
Run the full Vitest suite with coverage reporting:
- Distinctive: line 62
After running, check the coverage report:
- Distinctive: line 83
Create the contract test framework:
- Distinctive: line 138
Write contract tests for each endpoint:
- Distinctive: line 238
Create the k6 load test script:
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
API Testing and Verification (Phase 4)
Purpose
Phase 4 runs three layers of testing to verify the API implementation is correct, spec-compliant, and performant. Integration tests validate behaviour, contract tests verify OpenAPI spec conformance, and load tests check performance under concurrent usage. A unified test report is produced at the end.
When to Use
- Phase 3 (route-generation) is complete and all unit/integration tests pass.
- The user says "verify", "run tests", "check compliance", "load test", or "contract test".
- Before deployment or after significant changes.
Inputs
| Input | Required | Description |
| Passing integration tests | Yes | From Phase 2/3 |
| OpenAPI spec | Yes | .claude/plans/openapi.yaml or generated from app |
| Running API instance | For load tests | Local dev server at http://localhost:8787 |
Steps
Step 1 -- Integration Tests with Coverage
Run the full Vitest suite with coverage reporting:
cd api && pnpm vitest run --coverage --reporter=verboseCoverage thresholds are enforced in vitest.config.ts:
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html', 'lcov'],
include: ['src/**/*.ts'],
exclude: ['src/db/migrations/**', 'src/**/*.d.ts'],
thresholds: {
statements: 80,
branches: 75,
functions: 80,
lines: 80,
},
},After running, check the coverage report:
# Open HTML coverage report
open api/coverage/index.html
# Check JSON coverage for CI
cat api/coverage/coverage-summary.json | jq '.total'Step 2 -- Contract Tests (OpenAPI Spec Compliance)
Contract tests validate that every response from the API matches the OpenAPI specification. This catches schema drift between the spec and the implementation.
Install contract testing dependencies:
cd api && pnpm add -D openapi-response-validator @apidevtools/swagger-parserCreate the contract test framework:
// api/tests/contract/contract-validator.ts
import SwaggerParser from '@apidevtools/swagger-parser';
import OpenAPIResponseValidator from 'openapi-response-validator';
export class ContractValidator {
private spec: any;
private validators: Map<string, OpenAPIResponseValidator> = new Map();
async load(specPath: string) {
this.spec = await SwaggerParser.dereference(specPath);
}
validateResponse(
method: string,
path: string,
statusCode: number,
body: unknown,
) {
const pathSpec = this.spec.paths?.[path];
if (!pathSpec) {
throw new Error(`Path ${path} not found in OpenAPI spec`);
}
const operationSpec = pathSpec[method.toLowerCase()];
if (!operationSpec) {
throw new Error(`Method ${method} not found for path ${path}`);
}
const key = `${method}:${path}:${statusCode}`;
if (!this.validators.has(key)) {
this.validators.set(
key,
new OpenAPIResponseValidator({
responses: operationSpec.responses,
definitions: this.spec.components?.schemas,
}),
);
}
const validator = this.validators.get(key)!;
const errors = validator.validateResponse(statusCode, body);
if (errors) {
throw new Error(
`Contract violation for ${method} ${path} (${statusCode}):\n` +
JSON.stringify(errors, null, 2),
);
}
}
}Write contract tests for each endpoint:
// api/tests/contract/books.contract.test.ts
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { ContractValidator } from './contract-validator';
import { getTestApp } from '../helpers/app';
import {
createTestBook,
createTestAdmin,
generateTestToken,
authHeader,
} from '../helpers';
describe('Books API Contract Tests', () => {
let validator: ContractValidator;
let app: ReturnType<typeof getTestApp>;
beforeAll(async () => {
validator = new ContractValidator();
await validator.load('./.claude/plans/openapi.yaml');
});
beforeEach(() => {
app = getTestApp();
});
it('GET /books response matches OpenAPI spec', async () => {
await createTestBook();
const res = await app.request('/books');
const body = await res.json();
expect(() =>
validator.validateResponse('GET', '/books', 200, body),
).not.toThrow();
});
it('GET /books/:id response matches OpenAPI spec', async () => {
const book = await createTestBook();
const res = await app.request(`/books/${book.id}`);
const body = await res.json();
expect(() =>
validator.validateResponse('GET', '/books/{id}', 200, body),
).not.toThrow();
});
it('POST /books 201 response matches OpenAPI spec', async () => {
const admin = await createTestAdmin();
const token = await generateTestToken(admin.id, 'admin');
const res = await app.request('/books', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...authHeader(token),
},
body: JSON.stringify({
title: 'Contract Test Book',
author: 'Author',
isbn: '9780000000099',
price: '19.99',
}),
});
const body = await res.json();
expect(() =>
validator.validateResponse('POST', '/books', 201, body),
).not.toThrow();
});
it('POST /books 400 error response matches OpenAPI spec', async () => {
const admin = await createTestAdmin();
const token = await generateTestToken(admin.id, 'admin');
const res = await app.request('/books', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...authHeader(token),
},
body: JSON.stringify({}),
});
const body = await res.json();
expect(() =>
validator.validateResponse('POST', '/books', 400, body),
).not.toThrow();
});
});Run contract tests:
cd api && pnpm vitest run tests/contract/ --reporter=verboseStep 3 -- Load Tests with k6
Create the k6 load test script:
// api/tests/load/k6-load-test.js
import http from 'k6/http';
import { check, sleep, group } from 'k6';
import { Rate, Trend } from 'k6/metrics';
const errorRate = new Rate('errors');
const listBooksTrend = new Trend('list_books_duration');
const getBookTrend = new Trend('get_book_duration');
const BASE_URL = __ENV.BASE_URL || 'http://localhost:8787';
const ADMIN_TOKEN = __ENV.ADMIN_TOKEN || '';
export const options = {
stages: [
{ duration: '30s', target: 10 },
{ duration: '1m', target: 50 },
{ duration: '2m', target: 100 },
{ duration: '30s', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<500', 'p(99)<1000'],
errors: ['rate<0.01'],
list_books_duration: ['p(95)<300'],
get_book_duration: ['p(95)<200'],
},
};
export default function () {
const headers = { 'Content-Type': 'application/json' };
group('Public Endpoints', () => {
// List books
const listRes = http.get(`${BASE_URL}/books?page=1&limit=20`, { headers });
listBooksTrend.add(listRes.timings.duration);
check(listRes, {
'list books status 200': (r) => r.status === 200,
'list books has data': (r) => JSON.parse(r.body).data !== undefined,
});
errorRate.add(listRes.status !== 200);
sleep(0.5);
// Get single book
const books = JSON.parse(listRes.body).data;
if (books.length > 0) {
const bookId = books[Math.floor(Math.random() * books.length)].id;
const getRes = http.get(`${BASE_URL}/books/${bookId}`, { headers });
getBookTrend.add(getRes.timings.duration);
check(getRes, {
'get book status 200': (r) => r.status === 200,
});
errorRate.add(getRes.status !== 200);
}
sleep(0.5);
// Health check
const healthRes = http.get(`${BASE_URL}/health`, { headers });
check(healthRes, {
'health check status 200': (r) => r.status === 200,
});
});
group('Auth Endpoints', () => {
const loginRes = http.post(
`${BASE_URL}/auth/login`,
JSON.stringify({
email: `loadtest-${__VU}@example.com`,
password: 'loadtestpassword123',
}),
{ headers },
);
check(loginRes, {
'login responds': (r) => r.status === 200 || r.status === 401,
});
sleep(0.5);
});
}Run load tests:
# Start the API server first
cd api && pnpm dev &
# Run k6 load test
k6 run api/tests/load/k6-load-test.js \
-e BASE_URL=http://localhost:8787 \
-e ADMIN_TOKEN="your-admin-jwt-here"Step 4 -- Generate Unified Test Report
// scripts/generate-test-report.ts
interface TestReport {
generatedAt: string;
integration: {
passed: number;
failed: number;
skipped: number;
coverage: {
statements: number;
branches: number;
functions: number;
lines: number;
};
};
contract: {
passed: number;
failed: number;
violations: string[];
};
load: {
totalRequests: number;
avgResponseTime: number;
p95ResponseTime: number;
p99ResponseTime: number;
errorRate: number;
maxVUs: number;
duration: string;
};
verdict: 'PASS' | 'FAIL';
failures: string[];
}
function generateReport(
integrationResults: any,
contractResults: any,
loadResults: any,
): TestReport {
const failures: string[] = [];
if (integrationResults.numFailedTests > 0) {
failures.push(
`${integrationResults.numFailedTests} integration tests failed`,
);
}
const coverage = integrationResults.coverage;
if (coverage.statements < 80)
failures.push(`Statement coverage ${coverage.statements}% < 80%`);
if (coverage.branches < 75)
failures.push(`Branch coverage ${coverage.branches}% < 75%`);
if (contractResults.numFailedTests > 0) {
failures.push(
`${contractResults.numFailedTests} contract tests failed`,
);
}
if (loadResults.metrics.http_req_duration.p95 > 500) {
failures.push(
`p95 response time ${loadResults.metrics.http_req_duration.p95}ms > 500ms`,
);
}
if (loadResults.metrics.errors.rate > 0.01) {
failures.push(
`Error rate ${(loadResults.metrics.errors.rate * 100).toFixed(2)}% > 1%`,
);
}
return {
generatedAt: new Date().toISOString(),
integration: {
passed: integrationResults.numPassedTests,
failed: integrationResults.numFailedTests,
skipped: integrationResults.numPendingTests,
coverage,
},
contract: {
passed: contractResults.numPassedTests,
failed: contractResults.numFailedTests,
violations: contractResults.violations ?? [],
},
load: {
totalRequests: loadResults.metrics.http_reqs.count,
avgResponseTime: loadResults.metrics.http_req_duration.avg,
p95ResponseTime: loadResults.metrics.http_req_duration.p95,
p99ResponseTime: loadResults.metrics.http_req_duration.p99,
errorRate: loadResults.metrics.errors.rate,
maxVUs: loadResults.maxVUs,
duration: loadResults.duration,
},
verdict: failures.length === 0 ? 'PASS' : 'FAIL',
failures,
};
}Step 5 -- Run All Layers
# 1. Integration tests
cd api && pnpm vitest run --coverage --reporter=json --outputFile=test-results.json
# 2. Contract tests
cd api && pnpm vitest run tests/contract/ --reporter=json --outputFile=contract-results.json
# 3. Load tests (requires running server)
k6 run api/tests/load/k6-load-test.js --out json=api/tests/load/results.json
# 4. Generate report
cd api && pnpm tsx ../scripts/generate-test-report.tsOutput
| Artifact | Location | Description |
| Integration results | api/test-results.json | Vitest JSON report |
| Coverage report | api/coverage/ | HTML and JSON coverage |
| Contract results | api/contract-results.json | Contract test report |
| Load test results | api/tests/load/results.json | k6 metrics and summary |
| Unified report | .claude/plans/test-report.json | Combined pass/fail verdict |
Integration
| Skill | Relationship |
tdd-from-schema (Phase 2) | Integration tests written in Phase 2, executed here |
route-generation (Phase 3) | Implementations being tested |
deployment-config-generator (Phase 5) | Deployment proceeds only if verdict is PASS |
api-documentation | Contract tests verify spec accuracy for docs |
api-performance | Load test results may trigger performance optimization |
Gate Rule
Phase 5 (deployment) SHOULD NOT proceed if the test report verdict is FAIL.
Performance issues identified in load tests should trigger the api-performance
skill before deployment.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

