Add Jinja2 templates for test generation
This commit is contained in:
115
templates/pytest/test_base.py.j2
Normal file
115
templates/pytest/test_base.py.j2
Normal file
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
Generated pytest tests for {{ api_title }} API v{{ api_version }}
|
||||
|
||||
This file was auto-generated by API TestGen.
|
||||
"""
|
||||
import pytest
|
||||
import requests
|
||||
import json
|
||||
from jsonschema import validate, ValidationError
|
||||
|
||||
|
||||
BASE_URL = "{{ mock_server_url }}"
|
||||
|
||||
|
||||
{% if security_schemes %}
|
||||
{% for scheme_name, scheme in security_schemes.items() %}
|
||||
{% if scheme.type == "apiKey" %}
|
||||
@pytest.fixture
|
||||
def {{ scheme_name }}_headers():
|
||||
"""API Key authentication headers."""
|
||||
return {{"{{ scheme.name }}": "your-api-key"}}
|
||||
|
||||
|
||||
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
|
||||
@pytest.fixture
|
||||
def bearer_headers():
|
||||
"""Bearer token authentication headers."""
|
||||
return {{"Authorization": "Bearer your-token"}}
|
||||
|
||||
|
||||
{% elif scheme.type == "http" and scheme.scheme == "basic" %}
|
||||
@pytest.fixture
|
||||
def basic_headers():
|
||||
"""Basic authentication headers."""
|
||||
import base64
|
||||
credentials = "username:password"
|
||||
encoded = base64.b64encode(credentials.encode()).decode()
|
||||
return {{"Authorization": f"Basic {encoded}"}}
|
||||
|
||||
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% if definitions %}
|
||||
@pytest.fixture
|
||||
def base_url():
|
||||
"""Base URL for API requests."""
|
||||
return BASE_URL
|
||||
|
||||
|
||||
{% endif %}
|
||||
def validate_response(response, status_code=None):
|
||||
"""Validate API response.
|
||||
|
||||
Args:
|
||||
response: The response object.
|
||||
status_code: Expected status code (optional).
|
||||
"""
|
||||
if status_code:
|
||||
assert response.status_code == status_code, \
|
||||
f"Expected status {status_code}, got {response.status_code}"
|
||||
|
||||
assert response.headers.get("Content-Type", "").startswith("application/json"), \
|
||||
"Response Content-Type is not JSON"
|
||||
|
||||
|
||||
{% for endpoint in endpoints %}
|
||||
{% set endpoint_id = endpoint.operation_id or (endpoint.method + "_" + endpoint.path.strip("/").replace("/", "_").replace("{", "").replace("}", "")) %}
|
||||
def test_{{ endpoint_id }}(base_url{% if endpoint.parameters|selectattr("in", "equalto", "path")|list %}, {% for param in endpoint.parameters %}{% if param.in == "path" %}{{ param.name }}{% endif %}{% endfor %}{% endif %}):
|
||||
"""Test {{ endpoint.summary or endpoint.path }} endpoint.
|
||||
|
||||
{{ endpoint.description or "" }}
|
||||
"""
|
||||
url = f"{base_url}{{ endpoint.path }}"
|
||||
|
||||
headers = {{"Content-Type": "application/json"}}
|
||||
|
||||
{% if endpoint.security %}
|
||||
{% for security_requirement in endpoint.security %}
|
||||
{% for scheme_name in security_requirement.keys() %}
|
||||
{% if security_schemes[scheme_name].type == "apiKey" %}
|
||||
headers["{{ security_schemes[scheme_name].name }}"] = "test-api-key"
|
||||
{% elif security_schemes[scheme_name].type == "http" and security_schemes[scheme_name].scheme == "bearer" %}
|
||||
headers["Authorization"] = "Bearer test-token"
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% if endpoint.method in ["post", "put", "patch"] %}
|
||||
{% if endpoint.request_body %}
|
||||
request_body = {{}}
|
||||
{% else %}
|
||||
request_body = {{}}
|
||||
{% endif %}
|
||||
|
||||
response = requests.{{ endpoint["method"] }}(url, json=request_body, headers=headers)
|
||||
{% else %}
|
||||
response = requests.{{ endpoint["method"] }}(url, headers=headers{% if endpoint.parameters|selectattr("in", "equalto", "query")|list %}{% set query_params = endpoint.parameters|selectattr("in", "equalto", "query")|list %}{% if query_params %}, params={% for param in query_params %}"{{ param.name }}": "test"{% endfor %}{% endif %}{% endif %})
|
||||
{% endif %}
|
||||
|
||||
validate_response(response{% if endpoint.responses["200"] %}, {{ endpoint.responses["200"] }}{% endif %})
|
||||
|
||||
{% if endpoint.responses %}
|
||||
{% if endpoint.responses["200"] and endpoint.responses["200"].schema %}
|
||||
try:
|
||||
data = response.json()
|
||||
except json.JSONDecodeError:
|
||||
pytest.fail("Response is not valid JSON")
|
||||
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
Reference in New Issue
Block a user