pytest-openapi 0.1.1.dev202601050151__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_openapi/__init__.py +1 -0
- pytest_openapi/case_generator.py +433 -0
- pytest_openapi/contract.py +834 -0
- pytest_openapi/openapi.py +259 -0
- pytest_openapi/plugin.py +92 -0
- pytest_openapi-0.1.1.dev202601050151.dist-info/METADATA +182 -0
- pytest_openapi-0.1.1.dev202601050151.dist-info/RECORD +11 -0
- pytest_openapi-0.1.1.dev202601050151.dist-info/WHEEL +5 -0
- pytest_openapi-0.1.1.dev202601050151.dist-info/entry_points.txt +2 -0
- pytest_openapi-0.1.1.dev202601050151.dist-info/licenses/LICENSE +21 -0
- pytest_openapi-0.1.1.dev202601050151.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"""OpenAPI specification validation and parsing."""
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def check_request_body_has_example(method, path, operation):
|
|
9
|
+
"""Check if a POST/PUT/DELETE operation has a request body example
|
|
10
|
+
or schema.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
method: HTTP method (post, put, delete)
|
|
14
|
+
path: API endpoint path
|
|
15
|
+
operation: OpenAPI operation object
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
str or None: Error message if example AND schema are missing, None otherwise
|
|
19
|
+
"""
|
|
20
|
+
request_body = operation.get("requestBody", {})
|
|
21
|
+
content = request_body.get("content", {})
|
|
22
|
+
|
|
23
|
+
has_example_or_schema = False
|
|
24
|
+
for media_type, media_obj in content.items():
|
|
25
|
+
if (
|
|
26
|
+
"example" in media_obj
|
|
27
|
+
or "examples" in media_obj
|
|
28
|
+
or "schema" in media_obj
|
|
29
|
+
):
|
|
30
|
+
has_example_or_schema = True
|
|
31
|
+
break
|
|
32
|
+
|
|
33
|
+
if content and not has_example_or_schema:
|
|
34
|
+
return f" - {method.upper()} {path}: Missing request body example or schema"
|
|
35
|
+
|
|
36
|
+
return None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def check_operation_has_responses(method, path, operation):
|
|
40
|
+
"""Check if an operation has response definitions.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
method: HTTP method (get, post, put, delete)
|
|
44
|
+
path: API endpoint path
|
|
45
|
+
operation: OpenAPI operation object
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
str or None: Error message if responses are missing, None otherwise
|
|
49
|
+
"""
|
|
50
|
+
responses = operation.get("responses", {})
|
|
51
|
+
if not responses:
|
|
52
|
+
return f" - {method.upper()} {path}: Missing response definitions"
|
|
53
|
+
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def check_response_has_example(method, path, status_code, response_obj):
|
|
58
|
+
"""Check if a response definition has an example or schema.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
method: HTTP method (get, post, put, delete)
|
|
62
|
+
path: API endpoint path
|
|
63
|
+
status_code: HTTP status code
|
|
64
|
+
response_obj: OpenAPI response object
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
str or None: Error message if example AND schema are missing, None otherwise
|
|
68
|
+
"""
|
|
69
|
+
content = response_obj.get("content", {})
|
|
70
|
+
|
|
71
|
+
has_example_or_schema = False
|
|
72
|
+
for media_type, media_obj in content.items():
|
|
73
|
+
if (
|
|
74
|
+
"example" in media_obj
|
|
75
|
+
or "examples" in media_obj
|
|
76
|
+
or "schema" in media_obj
|
|
77
|
+
):
|
|
78
|
+
has_example_or_schema = True
|
|
79
|
+
break
|
|
80
|
+
|
|
81
|
+
if content and not has_example_or_schema:
|
|
82
|
+
return f" - {method.upper()} {path}: Missing response example or schema for status {status_code}"
|
|
83
|
+
|
|
84
|
+
return None
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def check_schema_descriptions(schema, path_prefix=""):
|
|
88
|
+
"""Recursively check that all fields in a schema have descriptions.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
schema: OpenAPI schema object
|
|
92
|
+
path_prefix: Current path in the schema (for error messages)
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
list: List of error messages for fields missing descriptions
|
|
96
|
+
"""
|
|
97
|
+
errors = []
|
|
98
|
+
|
|
99
|
+
if not isinstance(schema, dict):
|
|
100
|
+
return errors
|
|
101
|
+
|
|
102
|
+
# Check properties
|
|
103
|
+
properties = schema.get("properties", {})
|
|
104
|
+
for prop_name, prop_schema in properties.items():
|
|
105
|
+
prop_path = f"{path_prefix}.{prop_name}" if path_prefix else prop_name
|
|
106
|
+
|
|
107
|
+
# Check if this property has a description
|
|
108
|
+
if "description" not in prop_schema:
|
|
109
|
+
errors.append(f"Field '{prop_path}' is missing a description")
|
|
110
|
+
|
|
111
|
+
# Recursively check nested objects
|
|
112
|
+
if prop_schema.get("type") == "object":
|
|
113
|
+
nested_errors = check_schema_descriptions(prop_schema, prop_path)
|
|
114
|
+
errors.extend(nested_errors)
|
|
115
|
+
|
|
116
|
+
# Check items in arrays
|
|
117
|
+
elif prop_schema.get("type") == "array":
|
|
118
|
+
items = prop_schema.get("items", {})
|
|
119
|
+
if items.get("type") == "object":
|
|
120
|
+
nested_errors = check_schema_descriptions(
|
|
121
|
+
items, f"{prop_path}[]"
|
|
122
|
+
)
|
|
123
|
+
errors.extend(nested_errors)
|
|
124
|
+
|
|
125
|
+
return errors
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def check_endpoint_schema_descriptions(method, path, operation):
|
|
129
|
+
"""Check that all schema fields have descriptions for an endpoint.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
method: HTTP method (get, post, put, delete)
|
|
133
|
+
path: API endpoint path
|
|
134
|
+
operation: OpenAPI operation object
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
list: List of error messages for missing descriptions
|
|
138
|
+
"""
|
|
139
|
+
errors = []
|
|
140
|
+
|
|
141
|
+
# Check request body schema
|
|
142
|
+
if method in ["post", "put", "delete"]:
|
|
143
|
+
request_body = operation.get("requestBody", {})
|
|
144
|
+
content = request_body.get("content", {})
|
|
145
|
+
|
|
146
|
+
for media_type, media_obj in content.items():
|
|
147
|
+
schema = media_obj.get("schema", {})
|
|
148
|
+
if schema:
|
|
149
|
+
schema_errors = check_schema_descriptions(schema)
|
|
150
|
+
for error in schema_errors:
|
|
151
|
+
errors.append(
|
|
152
|
+
f" - {method.upper()} {path} request body: {error}"
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Check response schemas
|
|
156
|
+
responses = operation.get("responses", {})
|
|
157
|
+
for status_code, response_obj in responses.items():
|
|
158
|
+
content = response_obj.get("content", {})
|
|
159
|
+
|
|
160
|
+
for media_type, media_obj in content.items():
|
|
161
|
+
schema = media_obj.get("schema", {})
|
|
162
|
+
if schema:
|
|
163
|
+
schema_errors = check_schema_descriptions(schema)
|
|
164
|
+
for error in schema_errors:
|
|
165
|
+
errors.append(
|
|
166
|
+
f" - {method.upper()} {path} response {status_code}: {error}"
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
return errors
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def validate_openapi_spec(base_url):
|
|
173
|
+
"""Validate that the OpenAPI spec is available and meets
|
|
174
|
+
requirements.
|
|
175
|
+
|
|
176
|
+
Checks:
|
|
177
|
+
1. /openapi.json endpoint is accessible
|
|
178
|
+
2. All GET/POST/PUT/DELETE endpoints have request examples (where applicable)
|
|
179
|
+
3. All endpoints have response schemas
|
|
180
|
+
|
|
181
|
+
Args:
|
|
182
|
+
base_url: Base URL of the API server
|
|
183
|
+
|
|
184
|
+
Raises:
|
|
185
|
+
SystemExit: If validation fails
|
|
186
|
+
"""
|
|
187
|
+
openapi_url = f"{base_url}/openapi.json"
|
|
188
|
+
|
|
189
|
+
# Check 1: Fetch OpenAPI spec
|
|
190
|
+
try:
|
|
191
|
+
response = requests.get(openapi_url, timeout=10)
|
|
192
|
+
response.raise_for_status()
|
|
193
|
+
spec = response.json()
|
|
194
|
+
except requests.exceptions.RequestException as e:
|
|
195
|
+
print(f"\n❌ ERROR: Could not fetch OpenAPI spec from {openapi_url}")
|
|
196
|
+
print(f" Reason: {e}")
|
|
197
|
+
sys.exit(1)
|
|
198
|
+
except ValueError as e:
|
|
199
|
+
print(f"\n❌ ERROR: Invalid JSON in OpenAPI spec from {openapi_url}")
|
|
200
|
+
print(f" Reason: {e}")
|
|
201
|
+
sys.exit(1)
|
|
202
|
+
# TODO: Add JsonDecodeError handling
|
|
203
|
+
|
|
204
|
+
# Validate the spec structure
|
|
205
|
+
if "paths" not in spec:
|
|
206
|
+
print("\n❌ ERROR: OpenAPI spec missing 'paths' key")
|
|
207
|
+
sys.exit(1)
|
|
208
|
+
|
|
209
|
+
# Check 2 & 3: Validate endpoints
|
|
210
|
+
errors = []
|
|
211
|
+
paths = spec.get("paths", {})
|
|
212
|
+
|
|
213
|
+
for path, path_item in paths.items():
|
|
214
|
+
for method in ["get", "post", "put", "delete"]:
|
|
215
|
+
if method not in path_item:
|
|
216
|
+
continue
|
|
217
|
+
|
|
218
|
+
operation = path_item[method]
|
|
219
|
+
|
|
220
|
+
# Check request body examples for POST/PUT/DELETE
|
|
221
|
+
if method in ["post", "put", "delete"]:
|
|
222
|
+
error = check_request_body_has_example(method, path, operation)
|
|
223
|
+
if error:
|
|
224
|
+
errors.append(error)
|
|
225
|
+
|
|
226
|
+
# Check response schemas for all methods
|
|
227
|
+
error = check_operation_has_responses(method, path, operation)
|
|
228
|
+
if error:
|
|
229
|
+
errors.append(error)
|
|
230
|
+
continue
|
|
231
|
+
|
|
232
|
+
responses = operation.get("responses", {})
|
|
233
|
+
for status_code, response_obj in responses.items():
|
|
234
|
+
error = check_response_has_example(
|
|
235
|
+
method, path, status_code, response_obj
|
|
236
|
+
)
|
|
237
|
+
if error:
|
|
238
|
+
errors.append(error)
|
|
239
|
+
|
|
240
|
+
# Check schema field descriptions
|
|
241
|
+
description_errors = check_endpoint_schema_descriptions(
|
|
242
|
+
method, path, operation
|
|
243
|
+
)
|
|
244
|
+
errors.extend(description_errors)
|
|
245
|
+
|
|
246
|
+
# Report errors
|
|
247
|
+
if errors:
|
|
248
|
+
print("\n❌ ERROR: OpenAPI spec validation failed")
|
|
249
|
+
print("\nThe following endpoints have validation errors:")
|
|
250
|
+
for error in errors:
|
|
251
|
+
print(error)
|
|
252
|
+
print(
|
|
253
|
+
"\npytest-openapi requires proper schemas with descriptions for all fields, "
|
|
254
|
+
"and either examples or complete schemas for all endpoints."
|
|
255
|
+
)
|
|
256
|
+
sys.exit(1)
|
|
257
|
+
|
|
258
|
+
print(f"\n✅ OpenAPI spec validated successfully from {openapi_url}")
|
|
259
|
+
print(f" Found {len(paths)} path(s)")
|
pytest_openapi/plugin.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Pytest plugin for OpenAPI contract testing."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def pytest_addoption(parser):
|
|
5
|
+
"""Add --openapi CLI option."""
|
|
6
|
+
group = parser.getgroup("openapi")
|
|
7
|
+
group.addoption(
|
|
8
|
+
"--openapi",
|
|
9
|
+
action="store",
|
|
10
|
+
metavar="BASE_URL",
|
|
11
|
+
help="Run OpenAPI contract tests against the specified base URL",
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def pytest_configure(config):
|
|
16
|
+
"""Configure pytest with OpenAPI marker."""
|
|
17
|
+
config.addinivalue_line(
|
|
18
|
+
"markers",
|
|
19
|
+
"openapi: OpenAPI contract tests",
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
# If --openapi flag is provided, validate and test the OpenAPI spec
|
|
23
|
+
base_url = config.getoption("--openapi")
|
|
24
|
+
if base_url:
|
|
25
|
+
import pytest
|
|
26
|
+
import requests
|
|
27
|
+
|
|
28
|
+
from .contract import (
|
|
29
|
+
get_test_report,
|
|
30
|
+
test_delete_endpoint,
|
|
31
|
+
test_get_endpoint,
|
|
32
|
+
test_post_endpoint,
|
|
33
|
+
test_put_endpoint,
|
|
34
|
+
)
|
|
35
|
+
from .openapi import validate_openapi_spec
|
|
36
|
+
|
|
37
|
+
# Run validation checks
|
|
38
|
+
validate_openapi_spec(base_url)
|
|
39
|
+
|
|
40
|
+
# Fetch the OpenAPI spec
|
|
41
|
+
try:
|
|
42
|
+
response = requests.get(f"{base_url}/openapi.json", timeout=10)
|
|
43
|
+
response.raise_for_status()
|
|
44
|
+
spec = response.json()
|
|
45
|
+
except Exception as e:
|
|
46
|
+
pytest.exit(f"\n❌ Failed to fetch OpenAPI spec: {e}", returncode=1)
|
|
47
|
+
|
|
48
|
+
# Run contract tests on all endpoints
|
|
49
|
+
paths = spec.get("paths", {})
|
|
50
|
+
errors = []
|
|
51
|
+
|
|
52
|
+
for path, path_item in paths.items():
|
|
53
|
+
for method in ["get", "post", "put", "delete"]:
|
|
54
|
+
if method not in path_item:
|
|
55
|
+
continue
|
|
56
|
+
|
|
57
|
+
operation = path_item[method]
|
|
58
|
+
|
|
59
|
+
# Select appropriate test function
|
|
60
|
+
if method == "get":
|
|
61
|
+
success, error = test_get_endpoint(
|
|
62
|
+
base_url, path, operation
|
|
63
|
+
)
|
|
64
|
+
elif method == "post":
|
|
65
|
+
success, error = test_post_endpoint(
|
|
66
|
+
base_url, path, operation
|
|
67
|
+
)
|
|
68
|
+
elif method == "put":
|
|
69
|
+
success, error = test_put_endpoint(
|
|
70
|
+
base_url, path, operation
|
|
71
|
+
)
|
|
72
|
+
elif method == "delete":
|
|
73
|
+
success, error = test_delete_endpoint(
|
|
74
|
+
base_url, path, operation
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
if not success:
|
|
78
|
+
errors.append(f" {method.upper()} {path}: {error}")
|
|
79
|
+
|
|
80
|
+
# Generate and display human-readable report
|
|
81
|
+
print("\n")
|
|
82
|
+
print(get_test_report())
|
|
83
|
+
|
|
84
|
+
# Report results and exit
|
|
85
|
+
if errors:
|
|
86
|
+
print("\n❌ Contract tests failed:")
|
|
87
|
+
for error in errors:
|
|
88
|
+
print(error)
|
|
89
|
+
pytest.exit("Contract tests failed", returncode=1)
|
|
90
|
+
else:
|
|
91
|
+
print("\n✅ All contract tests passed!")
|
|
92
|
+
# Don't call sys.exit(0) - let pytest finish normally
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-openapi
|
|
3
|
+
Version: 0.1.1.dev202601050151
|
|
4
|
+
Author-email: Sinan Ozel <coding@sinan.slmail.me>
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2025 Sinan Ozel
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Project-URL: Homepage, https://github.com/sinan-ozel/pytest-openapi
|
|
28
|
+
Project-URL: Issues, https://github.com/sinan-ozel/pytest-openapi/issues
|
|
29
|
+
Classifier: Programming Language :: Python :: 3
|
|
30
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
31
|
+
Classifier: Operating System :: OS Independent
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Requires-Dist: pytest>=7.0.0
|
|
35
|
+
Requires-Dist: requests>=2.31.0
|
|
36
|
+
Requires-Dist: exrex>=0.11.0
|
|
37
|
+
Provides-Extra: test
|
|
38
|
+
Requires-Dist: pytest>=7.0.0; extra == "test"
|
|
39
|
+
Requires-Dist: pytest-depends>=1.0.1; extra == "test"
|
|
40
|
+
Requires-Dist: pytest-mock>=3.14.0; extra == "test"
|
|
41
|
+
Requires-Dist: httpx>=0.28.1; extra == "test"
|
|
42
|
+
Provides-Extra: dev
|
|
43
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
44
|
+
Requires-Dist: ruff>=0.12.11; extra == "dev"
|
|
45
|
+
Requires-Dist: black>=24.0.0; extra == "dev"
|
|
46
|
+
Requires-Dist: docformatter>=1.7.5; extra == "dev"
|
|
47
|
+
Provides-Extra: docs
|
|
48
|
+
Requires-Dist: mkdocs-material>=9.0.0; extra == "docs"
|
|
49
|
+
Requires-Dist: mkdocstrings[python]>=0.24.0; extra == "docs"
|
|
50
|
+
Provides-Extra: publish
|
|
51
|
+
Requires-Dist: packaging>=25.0; extra == "publish"
|
|
52
|
+
Dynamic: license-file
|
|
53
|
+
|
|
54
|
+

|
|
55
|
+

|
|
56
|
+

|
|
57
|
+

|
|
58
|
+
[](https://sinan-ozel.github.io/pytest-openapi/)
|
|
59
|
+

|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
# 🧪 OpenAPI Contract Tester
|
|
63
|
+
|
|
64
|
+
An opinionated, lightweight **black-box contract tester** against a **live API** using its OpenAPI specification as the source of truth.
|
|
65
|
+
|
|
66
|
+
This tool validates OpenAPI quality, generates test cases from schemas, and verifies that real HTTP responses match the contract.
|
|
67
|
+
This "certifies" that the documentation is complete with descriptions, example, and schema, and that the endpoint behaves as the documentation suggests.
|
|
68
|
+
|
|
69
|
+
## Why?
|
|
70
|
+
|
|
71
|
+
This package tries to simulate the frustrations of API users, as consumers.
|
|
72
|
+
With the rise of "agents", this type of documentation-code match became even
|
|
73
|
+
more important, because LLMs really have trouble choosing tools or using them
|
|
74
|
+
properly when they do not work as intended.
|
|
75
|
+
|
|
76
|
+
## ✨ What it does
|
|
77
|
+
|
|
78
|
+
### ▶️ Quick Example
|
|
79
|
+
|
|
80
|
+

|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pytest --openapi=http://localhost:8000
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
Test #10 ✅
|
|
88
|
+
POST /email
|
|
89
|
+
Requested:
|
|
90
|
+
{
|
|
91
|
+
"body": "Lorem ipsum dolor sit amet",
|
|
92
|
+
"from": "Lorem ipsum dolor sit amet",
|
|
93
|
+
"subject": "Lorem ipsum dolor sit amet",
|
|
94
|
+
"to": "Test!@#$%^&*()_+-=[]{}|;:<>?,./`~"
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
Expected 201
|
|
98
|
+
{
|
|
99
|
+
"body": "Hi Bob, how are you?",
|
|
100
|
+
"from": "alice@example.com",
|
|
101
|
+
"id": 1,
|
|
102
|
+
"subject": "Hello",
|
|
103
|
+
"to": "bob@example.com"
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
Actual 201
|
|
107
|
+
{
|
|
108
|
+
"body": "Lorem ipsum dolor sit amet",
|
|
109
|
+
"from": "Lorem ipsum dolor sit amet",
|
|
110
|
+
"id": 10,
|
|
111
|
+
"subject": "Lorem ipsum dolor sit amet",
|
|
112
|
+
"to": "Test!@#$%^&*()_+-=[]{}|;:<>?,./`~"
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
Generates multiple QA tests.
|
|
117
|
+
|
|
118
|
+
✔️ Validates OpenAPI request/response definitions
|
|
119
|
+
✔️ Enforces schema field descriptions
|
|
120
|
+
✔️ Generates test cases from schemas, checks response codes and types in the response
|
|
121
|
+
✔️ Tests the exanples
|
|
122
|
+
✔️ Tests **GET / POST / PUT / DELETE** endpoints
|
|
123
|
+
✔️ Compares live responses against examples
|
|
124
|
+
✔️ Produces a readable test report
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# ▶️ Detailed Example
|
|
128
|
+
|
|
129
|
+
## Install
|
|
130
|
+
```bash
|
|
131
|
+
pip install pytest-openapi
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Run
|
|
135
|
+
|
|
136
|
+
Say that you have a service running at port `8000` on `localhost`. Then, run:
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
pytest --openapi=http://localhost:8000
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Server
|
|
143
|
+
See here an example server - `email-server`: [tests/test_servers/email_server/server.py](tests/test_servers/email_server/server.py)
|
|
144
|
+
|
|
145
|
+
## Resulting Tests
|
|
146
|
+
|
|
147
|
+
[tests/test_servers/email_server/email_test_output.txt](tests/test_servers/email_server/email_test_output.txt)
|
|
148
|
+
|
|
149
|
+
# Future Plans / TODO
|
|
150
|
+
|
|
151
|
+
This is a work in progress.
|
|
152
|
+
- [ ] A check that the example matches the schema
|
|
153
|
+
- [ ] Ask that 400 responses be in the documentation.
|
|
154
|
+
- [ ] A check for regexp and email formats.
|
|
155
|
+
- [ ] Extra checks from 200 or 201 messages with missing keys to see 400 messages.
|
|
156
|
+
- [ ] Option to turn off the description requirement.
|
|
157
|
+
|
|
158
|
+
## In Consideration
|
|
159
|
+
- [ ] Use LLM-as-a-judge to assess the error messages and check their spelling.
|
|
160
|
+
|
|
161
|
+
# Contributing
|
|
162
|
+
Contributions are welcome!
|
|
163
|
+
|
|
164
|
+
The only requirement is 🐳 Docker.
|
|
165
|
+
|
|
166
|
+
Test are containerized, run them using the VS Code task `test`. If you don't want to use VS Code, the command is `docker compose -f ./tests/docker-compose.yaml --project-directory ./tests up --build --abort-on-container-exit --exit-code-from test`. Run this before making a PR, please.
|
|
167
|
+
|
|
168
|
+
There is also a development environment for VS Code, if you need it. On this environment, you can run the task `run-mock-server` to run one of the [mock servers](tests/test_servers) and see the output.
|
|
169
|
+
|
|
170
|
+
You can add your own mock server, and then add integration tests. Just follow the same pattern as every test to make a call - `subprocess.run('pytest', '--openapi=http://your-server:8000`.
|
|
171
|
+
|
|
172
|
+
Please reformat and lint before making a PR. The VS Task is `lint`, and if you don't want to use VS Code, the command is: `docker compose -f ./lint/docker-compose.yaml --project-directory ./lint up --build --abort-on-container-exit --exit-code-from linter`. Run this before making a PR, please.
|
|
173
|
+
|
|
174
|
+
If you add a functionality, please add to the the documentation.
|
|
175
|
+
|
|
176
|
+
Please submit a pull request or open an issue for any bugs or feature requests.
|
|
177
|
+
|
|
178
|
+
The moment your PR is merged, you get a dev release. You can then set up the version number to use your changes.
|
|
179
|
+
|
|
180
|
+
# License
|
|
181
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
182
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
pytest_openapi/__init__.py,sha256=mi83Zkryg5sd8L5o1QhMUJtIerdG3p6UWysB0tY7fOk,38
|
|
2
|
+
pytest_openapi/case_generator.py,sha256=vi_PtPgAhHliO6po2SYOmZNJFpyWGi0rYjg-q-kz6bc,13851
|
|
3
|
+
pytest_openapi/contract.py,sha256=Le_EdJgfgYR4MCa7N7FYAI4eNbhVuPHpdC5gPMyZV0k,27246
|
|
4
|
+
pytest_openapi/openapi.py,sha256=dlGw_8GHI_qrQa4Y82BFgKRzrfAat878fCZkXbmeR3E,8328
|
|
5
|
+
pytest_openapi/plugin.py,sha256=vrvOQm2yOKdDl3eFvyVfkLMBGkQurqwqAyTvEBQyvPo,2954
|
|
6
|
+
pytest_openapi-0.1.1.dev202601050151.dist-info/licenses/LICENSE,sha256=uCWQbz0H2dwv5L630wv0BEcZOs9hWrOj1lR7mPnEnV8,1067
|
|
7
|
+
pytest_openapi-0.1.1.dev202601050151.dist-info/METADATA,sha256=spp5jzHq0fCNVt4UIudRYh4QlFkpvwqAfmVF96NNCNo,7249
|
|
8
|
+
pytest_openapi-0.1.1.dev202601050151.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
9
|
+
pytest_openapi-0.1.1.dev202601050151.dist-info/entry_points.txt,sha256=PGXqI13KlKF_FMiu_Bfwg7IsiLP8kjZ85W5cYxAvVZk,50
|
|
10
|
+
pytest_openapi-0.1.1.dev202601050151.dist-info/top_level.txt,sha256=uxNtTL00ENrH7IA6TwaBpMKzllTkGIQ7Z2WTbkEmn3M,15
|
|
11
|
+
pytest_openapi-0.1.1.dev202601050151.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Sinan Ozel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pytest_openapi
|