5 / 89 · 04 API & Contract Testing with AI · From OpenAPI Schema to Test Suite← prev⊞ allnext →☰ Read as one page
1.5Automating the Pipeline
class SchemaToTestPipeline:
"""Automated pipeline: OpenAPI schema → test suite."""
def __init__(self, llm, schema_path: str, output_dir: str):
self.llm = llm
self.schema = self.load_schema(schema_path)
self.output_dir = output_dir
def generate_all(self):
"""Generate test files for every endpoint in the schema."""
endpoints = self.parse_endpoints(self.schema)
for endpoint in endpoints:
test_code = self.llm.generate(f"""
Generate pytest + httpx tests for:
Endpoint: {endpoint.method} {endpoint.path}
Parameters: {endpoint.parameters}
Request body: {endpoint.request_body}
Responses: {endpoint.responses}
Auth: {endpoint.security}
Full schema context:
{json.dumps(self.schema['components']['schemas'], indent=2)}
Include: happy path, all error codes, boundary values,
type mismatches, missing required fields, auth tests.
""")
# Save to file
filename = self.endpoint_to_filename(endpoint)
filepath = os.path.join(self.output_dir, filename)
with open(filepath, "w") as f:
f.write(test_code)
print(f"Generated: {filepath} ({endpoint.method} {endpoint.path})")
def endpoint_to_filename(self, endpoint) -> str:
"""Convert endpoint to a test filename."""
safe_path = endpoint.path.replace("/", "_").replace("{", "").replace("}", "")
return f"test_{endpoint.method.lower()}{safe_path}.py"