pytest-api-core 1.0.2__tar.gz → 1.0.4__tar.gz
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_api_core-1.0.4/MANIFEST.in +2 -0
- pytest_api_core-1.0.4/PKG-INFO +254 -0
- pytest_api_core-1.0.4/README.md +188 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/pyproject.toml +9 -6
- pytest_api_core-1.0.4/src/pytest_api_core/__init__.py +41 -0
- pytest_api_core-1.0.4/src/pytest_api_core/assertions/__init__.py +24 -0
- pytest_api_core-1.0.4/src/pytest_api_core/assertions/assert_utils.py +114 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/assertions/response_assertions.py +72 -35
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/client/api_client.py +34 -11
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/client/api_response.py +10 -2
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/config/base_settings.py +16 -1
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/config/config_manager.py +20 -9
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/fixtures/api_fixtures.py +62 -15
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/plugin.py +56 -15
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/reporters/html_reporter.py +108 -56
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/reporters/templates/report.html +78 -7
- pytest_api_core-1.0.4/src/pytest_api_core.egg-info/PKG-INFO +254 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core.egg-info/SOURCES.txt +2 -3
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core.egg-info/requires.txt +5 -5
- pytest_api_core-1.0.2/MANIFEST.in +0 -4
- pytest_api_core-1.0.2/PKG-INFO +0 -317
- pytest_api_core-1.0.2/README.md +0 -254
- pytest_api_core-1.0.2/src/pytest_api_core/__init__.py +0 -20
- pytest_api_core-1.0.2/src/pytest_api_core/assertions/__init__.py +0 -3
- pytest_api_core-1.0.2/src/pytest_api_core.egg-info/PKG-INFO +0 -317
- pytest_api_core-1.0.2/tests/test_sample_api.py +0 -150
- pytest_api_core-1.0.2/tests/test_unit.py +0 -187
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/LICENSE +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/setup.cfg +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/auth/__init__.py +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/auth/auth_handlers.py +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/client/__init__.py +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/config/__init__.py +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/config/env_loader.py +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/fixtures/__init__.py +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core/reporters/__init__.py +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core.egg-info/dependency_links.txt +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core.egg-info/entry_points.txt +0 -0
- {pytest_api_core-1.0.2 → pytest_api_core-1.0.4}/src/pytest_api_core.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-api-core
|
|
3
|
+
Version: 1.0.4
|
|
4
|
+
Summary: Pytest API automation framework with custom HTML reporting, reusable fixtures, and fluent assertions
|
|
5
|
+
Author: QA Platform Team
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Subodh Singh
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/sksingh329/pytest-api-core-framework
|
|
29
|
+
Project-URL: Repository, https://github.com/sksingh329/pytest-api-core-framework
|
|
30
|
+
Project-URL: Documentation, https://github.com/sksingh329/pytest-api-core-framework/blob/develop/README.md
|
|
31
|
+
Project-URL: Bug Tracker, https://github.com/sksingh329/pytest-api-core-framework/issues
|
|
32
|
+
Project-URL: Changelog, https://github.com/sksingh329/pytest-api-core-framework/blob/develop/CHANGELOG.md
|
|
33
|
+
Keywords: pytest,api,testing,automation,html-report
|
|
34
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
35
|
+
Classifier: Framework :: Pytest
|
|
36
|
+
Classifier: Intended Audience :: Developers
|
|
37
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
38
|
+
Classifier: Topic :: Software Development :: Testing
|
|
39
|
+
Classifier: Programming Language :: Python :: 3
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
46
|
+
Classifier: Operating System :: OS Independent
|
|
47
|
+
Requires-Python: >=3.9
|
|
48
|
+
Description-Content-Type: text/markdown
|
|
49
|
+
License-File: LICENSE
|
|
50
|
+
Requires-Dist: pytest<10.0,>=7.4
|
|
51
|
+
Requires-Dist: requests<3.0,>=2.31
|
|
52
|
+
Requires-Dist: Jinja2<4.0,>=3.1
|
|
53
|
+
Requires-Dist: urllib3<3.0,>=2.0
|
|
54
|
+
Requires-Dist: jsonschema<5.0,>=4.20
|
|
55
|
+
Provides-Extra: dotenv
|
|
56
|
+
Requires-Dist: python-dotenv>=1.0; extra == "dotenv"
|
|
57
|
+
Provides-Extra: dev
|
|
58
|
+
Requires-Dist: pytest-mock>=3.12; extra == "dev"
|
|
59
|
+
Requires-Dist: responses>=0.25; extra == "dev"
|
|
60
|
+
Requires-Dist: black>=24.0; extra == "dev"
|
|
61
|
+
Requires-Dist: isort>=5.13; extra == "dev"
|
|
62
|
+
Requires-Dist: mypy>=1.8; extra == "dev"
|
|
63
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
64
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
65
|
+
Dynamic: license-file
|
|
66
|
+
|
|
67
|
+
# pytest-api-core
|
|
68
|
+
|
|
69
|
+
[](https://pypi.org/project/pytest-api-core/)
|
|
70
|
+
[](https://pypi.org/project/pytest-api-core/)
|
|
71
|
+
[](LICENSE)
|
|
72
|
+
|
|
73
|
+
> A reusable pytest plugin for API automation — fluent assertions, built-in auth strategies, environment-aware configuration, and a self-contained HTML report.
|
|
74
|
+
|
|
75
|
+
## Features
|
|
76
|
+
|
|
77
|
+
- **`APIClient`** — `requests.Session` wrapper with retry, timeout, and structured logging
|
|
78
|
+
- **Fluent assertions** — `assert_that(response).status_is(200).json_path("$.id").equals(1)`
|
|
79
|
+
(`json_path()` supports a small dot/bracket-index subset, not full JSONPath — see [docs/QUICKSTART.md](docs/QUICKSTART.md#json_path-syntax);
|
|
80
|
+
`response_time_under()` measures the whole request including any retries, not just the final attempt)
|
|
81
|
+
- **Auth strategies** — Bearer token, Basic, API Key (header/query), OAuth2 client credentials
|
|
82
|
+
(need something else, like an OAuth2 refresh-token flow or signed requests? subclass `AuthBase` — see [docs/QUICKSTART.md](docs/QUICKSTART.md#custom-auth-handlers))
|
|
83
|
+
- **Standalone assertion helpers** — `assert_equal`, `assert_contains`, `assert_matches`, `assert_matches_schema`, etc. for values outside an `APIResponse` — see [docs/QUICKSTART.md](docs/QUICKSTART.md#standalone-assertion-helpers)
|
|
84
|
+
- **Configurable retry policy** — max attempts, backoff factor, and retryable methods, overridable per environment (see below)
|
|
85
|
+
- **Environment config** — Python `settings.py` classes + `.env` file + env var overrides
|
|
86
|
+
- **Custom HTML report** — self-contained file with charts, filterable table, and request/response details
|
|
87
|
+
- **Auto-registered fixtures** — zero boilerplate in consuming projects
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Installation
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
pip install pytest-api-core==1.0.3
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
With `.env` file support (recommended):
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
pip install "pytest-api-core[dotenv]==1.0.3"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Quick start
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
# tests/test_posts.py
|
|
109
|
+
from pytest_api_core.assertions import assert_that
|
|
110
|
+
|
|
111
|
+
def test_get_post(api_client):
|
|
112
|
+
response = api_client.get("/posts/1")
|
|
113
|
+
assert_that(response).status_is(200).json_path("$.id").equals(1)
|
|
114
|
+
|
|
115
|
+
def test_create_post(api_client):
|
|
116
|
+
payload = {"title": "foo", "body": "bar", "userId": 1}
|
|
117
|
+
response = api_client.post("/posts", json=payload)
|
|
118
|
+
assert_that(response).status_is(201).has_key("id")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pytest tests/ --api-env=staging
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
For full setup instructions — including `pytest.ini`, `config/settings.py`, `conftest.py` auth overrides, and CI integration — see **[docs/QUICKSTART.md](docs/QUICKSTART.md)**.
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## Configuration
|
|
130
|
+
|
|
131
|
+
### Resolution order (highest → lowest priority)
|
|
132
|
+
|
|
133
|
+
| Priority | Source |
|
|
134
|
+
|---|---|
|
|
135
|
+
| 1 | Shell / CI environment variables (`API_BASE_URL`, `API_TOKEN`, …) |
|
|
136
|
+
| 2 | `--api-base-url` CLI flag |
|
|
137
|
+
| 3 | `ENVIRONMENTS[env]` class in `settings_module` |
|
|
138
|
+
| 4 | Built-in defaults (`http://localhost`, timeout 30 s) |
|
|
139
|
+
|
|
140
|
+
### Retry policy
|
|
141
|
+
|
|
142
|
+
`APIClient` retries idempotent requests (`GET`, `HEAD`, `OPTIONS`) on `500/502/503/504`
|
|
143
|
+
responses using `urllib3.Retry`. All three knobs follow the same env var → CLI flag →
|
|
144
|
+
`settings_module` → built-in default precedence as `base_url`:
|
|
145
|
+
|
|
146
|
+
| Setting | `settings_module` attribute | CLI flag | Env var | Default |
|
|
147
|
+
|---|---|---|---|---|
|
|
148
|
+
| Max retry attempts | `api_retry_total` | `--api-retry-total` | `API_RETRY_TOTAL` | `3` |
|
|
149
|
+
| Backoff factor | `api_retry_backoff_factor` | `--api-retry-backoff-factor` | `API_RETRY_BACKOFF_FACTOR` | `0.3` |
|
|
150
|
+
| Retryable methods | `api_retry_methods` | `--api-retry-methods` (comma-separated) | `API_RETRY_METHODS` (comma-separated) | `GET,HEAD,OPTIONS` |
|
|
151
|
+
|
|
152
|
+
Backoff factor `0.3` produces sleeps of `0s, 0.3s, 0.6s, 1.2s, ...` between attempts.
|
|
153
|
+
|
|
154
|
+
Example — a flakier staging environment gets more retries and a longer backoff:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
# settings.py
|
|
158
|
+
class StagingSettings(BaseSettings):
|
|
159
|
+
base_url = "https://api.staging.mycompany.com"
|
|
160
|
+
api_retry_total = 5
|
|
161
|
+
api_retry_backoff_factor = 1.0
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Or per-run, without touching code:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
pytest tests/ --api-env=staging --api-retry-total=5 --api-retry-backoff-factor=1.0
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
To disable retries entirely, pass `retry=None` directly when constructing an
|
|
171
|
+
`APIClient` yourself (not available as a config option, since "no retries" isn't
|
|
172
|
+
something you'd want to toggle per environment).
|
|
173
|
+
|
|
174
|
+
### Key `pytest.ini` options
|
|
175
|
+
|
|
176
|
+
| Option | Description | Default |
|
|
177
|
+
|---|---|---|
|
|
178
|
+
| `api_env` | Active environment name | `dev` |
|
|
179
|
+
| `api_settings_module` | Dotted path to your settings module | — |
|
|
180
|
+
| `api_dotenv_file` | Path to `.env` file | `.env` |
|
|
181
|
+
| `api_log_level` | Framework log level | `WARNING` |
|
|
182
|
+
| `api_html_report` | HTML report path (supports `{env}`, `{timestamp}`) | — |
|
|
183
|
+
| `api_html_theme` | Report theme: `light` or `dark` | `dark` |
|
|
184
|
+
| `api_html_title` | Report browser tab title | `API Test Report` |
|
|
185
|
+
| `api_html_header` | Report page header text | `API Test Report` |
|
|
186
|
+
|
|
187
|
+
### CLI flags
|
|
188
|
+
|
|
189
|
+
| Flag | Description |
|
|
190
|
+
|---|---|
|
|
191
|
+
| `--api-env` | Override active environment |
|
|
192
|
+
| `--api-base-url` | Override `base_url` |
|
|
193
|
+
| `--api-log-level` | Override framework log level |
|
|
194
|
+
| `--api-html-report` | Override HTML report path |
|
|
195
|
+
| `--api-retry-total` | Override max retry attempts (default: `3`) |
|
|
196
|
+
| `--api-retry-backoff-factor` | Override retry backoff factor (default: `0.3`) |
|
|
197
|
+
| `--api-retry-methods` | Override retryable HTTP methods, comma-separated (default: `GET,HEAD,OPTIONS`) |
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## Fixtures
|
|
202
|
+
|
|
203
|
+
All fixtures are auto-registered — no imports needed in `conftest.py`.
|
|
204
|
+
|
|
205
|
+
| Fixture | Scope | Description |
|
|
206
|
+
|---|---|---|
|
|
207
|
+
| `api_config` | session | Resolved config dict for the active env |
|
|
208
|
+
| `auth_provider` | session | Auth strategy passed to `api_client` — override this alone to swap auth |
|
|
209
|
+
| `api_client` | session | Configured `APIClient` instance, depends on `auth_provider` |
|
|
210
|
+
| `api_bearer_auth` | function | `BearerAuth` from `API_TOKEN` env var |
|
|
211
|
+
| `api_basic_auth` | function | `BasicAuth` from `API_USERNAME` / `API_PASSWORD` |
|
|
212
|
+
| `api_key_auth` | function | `APIKeyAuth` from `API_KEY_NAME` / `API_KEY_VALUE` |
|
|
213
|
+
|
|
214
|
+
Override `auth_provider` in your project's `conftest.py` to inject custom auth without
|
|
215
|
+
redeclaring `api_client` — see [docs/QUICKSTART.md](docs/QUICKSTART.md#7-override-auth--testsconftestpy).
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## Project structure
|
|
220
|
+
|
|
221
|
+
```
|
|
222
|
+
pytest-api-core-framework/
|
|
223
|
+
├── src/pytest_api_core/
|
|
224
|
+
│ ├── plugin.py # pytest11 entry point — CLI/ini options, .env loading, reporter registration
|
|
225
|
+
│ ├── assertions/
|
|
226
|
+
│ │ ├── response_assertions.py # assert_that(response) fluent chain
|
|
227
|
+
│ │ └── assert_utils.py # standalone helpers: assert_equal, assert_contains, ...
|
|
228
|
+
│ ├── auth/
|
|
229
|
+
│ │ └── auth_handlers.py # BearerAuth, BasicAuth, APIKeyAuth, OAuth2ClientCredentials
|
|
230
|
+
│ ├── client/
|
|
231
|
+
│ │ ├── api_client.py # APIClient (requests.Session wrapper, retry, __API_CALL__ logging)
|
|
232
|
+
│ │ └── api_response.py # APIResponse wrapper (adds elapsed_ms)
|
|
233
|
+
│ ├── config/
|
|
234
|
+
│ │ ├── base_settings.py # BaseSettings — subclass to define environments
|
|
235
|
+
│ │ ├── config_manager.py # ConfigManager — merges env vars / CLI / settings_module / defaults
|
|
236
|
+
│ │ └── env_loader.py # .env file loading
|
|
237
|
+
│ ├── fixtures/
|
|
238
|
+
│ │ └── api_fixtures.py # api_config, auth_provider, api_client, api_*_auth fixtures
|
|
239
|
+
│ └── reporters/
|
|
240
|
+
│ ├── html_reporter.py # parses __API_CALL__/__API_ASSERT__ sentinels, renders HTML report
|
|
241
|
+
│ └── templates/report.html
|
|
242
|
+
├── tests/
|
|
243
|
+
│ ├── unit/test_unit.py # mocked HTTP tests (responses library)
|
|
244
|
+
│ └── e2e/test_e2e_jsonplaceholder.py # live tests against jsonplaceholder.typicode.com
|
|
245
|
+
├── docs/QUICKSTART.md # full setup guide (settings.py, conftest.py, CI)
|
|
246
|
+
├── config/settings.py # example ENVIRONMENTS settings module
|
|
247
|
+
└── pyproject.toml
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# pytest-api-core
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/pytest-api-core/)
|
|
4
|
+
[](https://pypi.org/project/pytest-api-core/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
> A reusable pytest plugin for API automation — fluent assertions, built-in auth strategies, environment-aware configuration, and a self-contained HTML report.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **`APIClient`** — `requests.Session` wrapper with retry, timeout, and structured logging
|
|
12
|
+
- **Fluent assertions** — `assert_that(response).status_is(200).json_path("$.id").equals(1)`
|
|
13
|
+
(`json_path()` supports a small dot/bracket-index subset, not full JSONPath — see [docs/QUICKSTART.md](docs/QUICKSTART.md#json_path-syntax);
|
|
14
|
+
`response_time_under()` measures the whole request including any retries, not just the final attempt)
|
|
15
|
+
- **Auth strategies** — Bearer token, Basic, API Key (header/query), OAuth2 client credentials
|
|
16
|
+
(need something else, like an OAuth2 refresh-token flow or signed requests? subclass `AuthBase` — see [docs/QUICKSTART.md](docs/QUICKSTART.md#custom-auth-handlers))
|
|
17
|
+
- **Standalone assertion helpers** — `assert_equal`, `assert_contains`, `assert_matches`, `assert_matches_schema`, etc. for values outside an `APIResponse` — see [docs/QUICKSTART.md](docs/QUICKSTART.md#standalone-assertion-helpers)
|
|
18
|
+
- **Configurable retry policy** — max attempts, backoff factor, and retryable methods, overridable per environment (see below)
|
|
19
|
+
- **Environment config** — Python `settings.py` classes + `.env` file + env var overrides
|
|
20
|
+
- **Custom HTML report** — self-contained file with charts, filterable table, and request/response details
|
|
21
|
+
- **Auto-registered fixtures** — zero boilerplate in consuming projects
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install pytest-api-core==1.0.3
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
With `.env` file support (recommended):
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install "pytest-api-core[dotenv]==1.0.3"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
# tests/test_posts.py
|
|
43
|
+
from pytest_api_core.assertions import assert_that
|
|
44
|
+
|
|
45
|
+
def test_get_post(api_client):
|
|
46
|
+
response = api_client.get("/posts/1")
|
|
47
|
+
assert_that(response).status_is(200).json_path("$.id").equals(1)
|
|
48
|
+
|
|
49
|
+
def test_create_post(api_client):
|
|
50
|
+
payload = {"title": "foo", "body": "bar", "userId": 1}
|
|
51
|
+
response = api_client.post("/posts", json=payload)
|
|
52
|
+
assert_that(response).status_is(201).has_key("id")
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pytest tests/ --api-env=staging
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
For full setup instructions — including `pytest.ini`, `config/settings.py`, `conftest.py` auth overrides, and CI integration — see **[docs/QUICKSTART.md](docs/QUICKSTART.md)**.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
|
|
65
|
+
### Resolution order (highest → lowest priority)
|
|
66
|
+
|
|
67
|
+
| Priority | Source |
|
|
68
|
+
|---|---|
|
|
69
|
+
| 1 | Shell / CI environment variables (`API_BASE_URL`, `API_TOKEN`, …) |
|
|
70
|
+
| 2 | `--api-base-url` CLI flag |
|
|
71
|
+
| 3 | `ENVIRONMENTS[env]` class in `settings_module` |
|
|
72
|
+
| 4 | Built-in defaults (`http://localhost`, timeout 30 s) |
|
|
73
|
+
|
|
74
|
+
### Retry policy
|
|
75
|
+
|
|
76
|
+
`APIClient` retries idempotent requests (`GET`, `HEAD`, `OPTIONS`) on `500/502/503/504`
|
|
77
|
+
responses using `urllib3.Retry`. All three knobs follow the same env var → CLI flag →
|
|
78
|
+
`settings_module` → built-in default precedence as `base_url`:
|
|
79
|
+
|
|
80
|
+
| Setting | `settings_module` attribute | CLI flag | Env var | Default |
|
|
81
|
+
|---|---|---|---|---|
|
|
82
|
+
| Max retry attempts | `api_retry_total` | `--api-retry-total` | `API_RETRY_TOTAL` | `3` |
|
|
83
|
+
| Backoff factor | `api_retry_backoff_factor` | `--api-retry-backoff-factor` | `API_RETRY_BACKOFF_FACTOR` | `0.3` |
|
|
84
|
+
| Retryable methods | `api_retry_methods` | `--api-retry-methods` (comma-separated) | `API_RETRY_METHODS` (comma-separated) | `GET,HEAD,OPTIONS` |
|
|
85
|
+
|
|
86
|
+
Backoff factor `0.3` produces sleeps of `0s, 0.3s, 0.6s, 1.2s, ...` between attempts.
|
|
87
|
+
|
|
88
|
+
Example — a flakier staging environment gets more retries and a longer backoff:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
# settings.py
|
|
92
|
+
class StagingSettings(BaseSettings):
|
|
93
|
+
base_url = "https://api.staging.mycompany.com"
|
|
94
|
+
api_retry_total = 5
|
|
95
|
+
api_retry_backoff_factor = 1.0
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Or per-run, without touching code:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pytest tests/ --api-env=staging --api-retry-total=5 --api-retry-backoff-factor=1.0
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
To disable retries entirely, pass `retry=None` directly when constructing an
|
|
105
|
+
`APIClient` yourself (not available as a config option, since "no retries" isn't
|
|
106
|
+
something you'd want to toggle per environment).
|
|
107
|
+
|
|
108
|
+
### Key `pytest.ini` options
|
|
109
|
+
|
|
110
|
+
| Option | Description | Default |
|
|
111
|
+
|---|---|---|
|
|
112
|
+
| `api_env` | Active environment name | `dev` |
|
|
113
|
+
| `api_settings_module` | Dotted path to your settings module | — |
|
|
114
|
+
| `api_dotenv_file` | Path to `.env` file | `.env` |
|
|
115
|
+
| `api_log_level` | Framework log level | `WARNING` |
|
|
116
|
+
| `api_html_report` | HTML report path (supports `{env}`, `{timestamp}`) | — |
|
|
117
|
+
| `api_html_theme` | Report theme: `light` or `dark` | `dark` |
|
|
118
|
+
| `api_html_title` | Report browser tab title | `API Test Report` |
|
|
119
|
+
| `api_html_header` | Report page header text | `API Test Report` |
|
|
120
|
+
|
|
121
|
+
### CLI flags
|
|
122
|
+
|
|
123
|
+
| Flag | Description |
|
|
124
|
+
|---|---|
|
|
125
|
+
| `--api-env` | Override active environment |
|
|
126
|
+
| `--api-base-url` | Override `base_url` |
|
|
127
|
+
| `--api-log-level` | Override framework log level |
|
|
128
|
+
| `--api-html-report` | Override HTML report path |
|
|
129
|
+
| `--api-retry-total` | Override max retry attempts (default: `3`) |
|
|
130
|
+
| `--api-retry-backoff-factor` | Override retry backoff factor (default: `0.3`) |
|
|
131
|
+
| `--api-retry-methods` | Override retryable HTTP methods, comma-separated (default: `GET,HEAD,OPTIONS`) |
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Fixtures
|
|
136
|
+
|
|
137
|
+
All fixtures are auto-registered — no imports needed in `conftest.py`.
|
|
138
|
+
|
|
139
|
+
| Fixture | Scope | Description |
|
|
140
|
+
|---|---|---|
|
|
141
|
+
| `api_config` | session | Resolved config dict for the active env |
|
|
142
|
+
| `auth_provider` | session | Auth strategy passed to `api_client` — override this alone to swap auth |
|
|
143
|
+
| `api_client` | session | Configured `APIClient` instance, depends on `auth_provider` |
|
|
144
|
+
| `api_bearer_auth` | function | `BearerAuth` from `API_TOKEN` env var |
|
|
145
|
+
| `api_basic_auth` | function | `BasicAuth` from `API_USERNAME` / `API_PASSWORD` |
|
|
146
|
+
| `api_key_auth` | function | `APIKeyAuth` from `API_KEY_NAME` / `API_KEY_VALUE` |
|
|
147
|
+
|
|
148
|
+
Override `auth_provider` in your project's `conftest.py` to inject custom auth without
|
|
149
|
+
redeclaring `api_client` — see [docs/QUICKSTART.md](docs/QUICKSTART.md#7-override-auth--testsconftestpy).
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Project structure
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
pytest-api-core-framework/
|
|
157
|
+
├── src/pytest_api_core/
|
|
158
|
+
│ ├── plugin.py # pytest11 entry point — CLI/ini options, .env loading, reporter registration
|
|
159
|
+
│ ├── assertions/
|
|
160
|
+
│ │ ├── response_assertions.py # assert_that(response) fluent chain
|
|
161
|
+
│ │ └── assert_utils.py # standalone helpers: assert_equal, assert_contains, ...
|
|
162
|
+
│ ├── auth/
|
|
163
|
+
│ │ └── auth_handlers.py # BearerAuth, BasicAuth, APIKeyAuth, OAuth2ClientCredentials
|
|
164
|
+
│ ├── client/
|
|
165
|
+
│ │ ├── api_client.py # APIClient (requests.Session wrapper, retry, __API_CALL__ logging)
|
|
166
|
+
│ │ └── api_response.py # APIResponse wrapper (adds elapsed_ms)
|
|
167
|
+
│ ├── config/
|
|
168
|
+
│ │ ├── base_settings.py # BaseSettings — subclass to define environments
|
|
169
|
+
│ │ ├── config_manager.py # ConfigManager — merges env vars / CLI / settings_module / defaults
|
|
170
|
+
│ │ └── env_loader.py # .env file loading
|
|
171
|
+
│ ├── fixtures/
|
|
172
|
+
│ │ └── api_fixtures.py # api_config, auth_provider, api_client, api_*_auth fixtures
|
|
173
|
+
│ └── reporters/
|
|
174
|
+
│ ├── html_reporter.py # parses __API_CALL__/__API_ASSERT__ sentinels, renders HTML report
|
|
175
|
+
│ └── templates/report.html
|
|
176
|
+
├── tests/
|
|
177
|
+
│ ├── unit/test_unit.py # mocked HTTP tests (responses library)
|
|
178
|
+
│ └── e2e/test_e2e_jsonplaceholder.py # live tests against jsonplaceholder.typicode.com
|
|
179
|
+
├── docs/QUICKSTART.md # full setup guide (settings.py, conftest.py, CI)
|
|
180
|
+
├── config/settings.py # example ENVIRONMENTS settings module
|
|
181
|
+
└── pyproject.toml
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
See [LICENSE](LICENSE).
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pytest-api-core"
|
|
7
|
-
version = "1.0.
|
|
7
|
+
version = "1.0.4"
|
|
8
8
|
description = "Pytest API automation framework with custom HTML reporting, reusable fixtures, and fluent assertions"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { file = "LICENSE" }
|
|
@@ -14,21 +14,24 @@ classifiers = [
|
|
|
14
14
|
"Development Status :: 5 - Production/Stable",
|
|
15
15
|
"Framework :: Pytest",
|
|
16
16
|
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
17
18
|
"Topic :: Software Development :: Testing",
|
|
18
19
|
"Programming Language :: Python :: 3",
|
|
19
20
|
"Programming Language :: Python :: 3.9",
|
|
20
21
|
"Programming Language :: Python :: 3.10",
|
|
21
22
|
"Programming Language :: Python :: 3.11",
|
|
22
23
|
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
23
26
|
"Operating System :: OS Independent",
|
|
24
27
|
]
|
|
25
28
|
requires-python = ">=3.9"
|
|
26
29
|
dependencies = [
|
|
27
|
-
"pytest>=7.4",
|
|
28
|
-
"requests>=2.31",
|
|
29
|
-
"Jinja2>=3.1",
|
|
30
|
-
"urllib3>=2.0",
|
|
31
|
-
"jsonschema>=4.20",
|
|
30
|
+
"pytest>=7.4,<10.0",
|
|
31
|
+
"requests>=2.31,<3.0",
|
|
32
|
+
"Jinja2>=3.1,<4.0",
|
|
33
|
+
"urllib3>=2.0,<3.0",
|
|
34
|
+
"jsonschema>=4.20,<5.0",
|
|
32
35
|
]
|
|
33
36
|
|
|
34
37
|
[project.optional-dependencies]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""
|
|
2
|
+
pytest-api-core: Reusable API automation framework for pytest.
|
|
3
|
+
Auto-registered as a pytest plugin via entry_points["pytest11"].
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from pytest_api_core.assertions.assert_utils import (
|
|
7
|
+
assert_contains,
|
|
8
|
+
assert_equal,
|
|
9
|
+
assert_equal_ignore_case,
|
|
10
|
+
assert_is_empty,
|
|
11
|
+
assert_is_not_empty,
|
|
12
|
+
assert_matches,
|
|
13
|
+
assert_not_equal,
|
|
14
|
+
)
|
|
15
|
+
from pytest_api_core.assertions.response_assertions import assert_that
|
|
16
|
+
from pytest_api_core.auth.auth_handlers import (
|
|
17
|
+
APIKeyAuth,
|
|
18
|
+
BasicAuth,
|
|
19
|
+
BearerAuth,
|
|
20
|
+
OAuth2ClientCredentials,
|
|
21
|
+
)
|
|
22
|
+
from pytest_api_core.client.api_client import APIClient
|
|
23
|
+
from pytest_api_core.client.api_response import APIResponse
|
|
24
|
+
|
|
25
|
+
__version__ = "1.0.0"
|
|
26
|
+
__all__ = [
|
|
27
|
+
"APIClient",
|
|
28
|
+
"APIResponse",
|
|
29
|
+
"assert_that",
|
|
30
|
+
"assert_equal",
|
|
31
|
+
"assert_not_equal",
|
|
32
|
+
"assert_equal_ignore_case",
|
|
33
|
+
"assert_contains",
|
|
34
|
+
"assert_matches",
|
|
35
|
+
"assert_is_empty",
|
|
36
|
+
"assert_is_not_empty",
|
|
37
|
+
"BearerAuth",
|
|
38
|
+
"BasicAuth",
|
|
39
|
+
"APIKeyAuth",
|
|
40
|
+
"OAuth2ClientCredentials",
|
|
41
|
+
]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from pytest_api_core.assertions.assert_utils import (
|
|
2
|
+
assert_contains,
|
|
3
|
+
assert_equal,
|
|
4
|
+
assert_equal_ignore_case,
|
|
5
|
+
assert_is_empty,
|
|
6
|
+
assert_is_not_empty,
|
|
7
|
+
assert_matches,
|
|
8
|
+
assert_matches_schema,
|
|
9
|
+
assert_not_equal,
|
|
10
|
+
)
|
|
11
|
+
from pytest_api_core.assertions.response_assertions import ResponseAssertions, assert_that
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"ResponseAssertions",
|
|
15
|
+
"assert_that",
|
|
16
|
+
"assert_equal",
|
|
17
|
+
"assert_not_equal",
|
|
18
|
+
"assert_equal_ignore_case",
|
|
19
|
+
"assert_contains",
|
|
20
|
+
"assert_matches",
|
|
21
|
+
"assert_matches_schema",
|
|
22
|
+
"assert_is_empty",
|
|
23
|
+
"assert_is_not_empty",
|
|
24
|
+
]
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generic, standalone string assertion utilities.
|
|
3
|
+
|
|
4
|
+
Usage
|
|
5
|
+
-----
|
|
6
|
+
from pytest_api_core.assertions import assert_equal
|
|
7
|
+
|
|
8
|
+
assert_equal(actual, expected)
|
|
9
|
+
assert_contains(actual, "substring")
|
|
10
|
+
assert_matches(actual, r"^\\d+$")
|
|
11
|
+
assert_matches_schema(actual, schema)
|
|
12
|
+
|
|
13
|
+
Unlike :class:`ResponseAssertions`, these are plain functions for comparing
|
|
14
|
+
arbitrary actual/expected strings — not tied to an ``APIResponse``.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import json
|
|
20
|
+
import logging
|
|
21
|
+
import re
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
_assert_log = logging.getLogger("pytest_api_core.assertions")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _emit(name: str, passed: bool, **detail: object) -> None:
|
|
28
|
+
"""Emit a machine-readable sentinel line captured by HTMLReporter."""
|
|
29
|
+
_assert_log.debug(
|
|
30
|
+
"__API_ASSERT__ %s",
|
|
31
|
+
json.dumps({"name": name, "passed": passed, **detail}, separators=(",", ":")),
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _fail(message: str | None, default: str) -> None:
|
|
36
|
+
raise AssertionError(f"{message}\n {default}" if message else default)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def assert_equal(actual: str, expected: str, *, message: str | None = None) -> None:
|
|
40
|
+
"""Assert *actual* equals *expected*."""
|
|
41
|
+
passed = actual == expected
|
|
42
|
+
_emit("assert_equal", passed, expected=expected, actual=actual)
|
|
43
|
+
if not passed:
|
|
44
|
+
_fail(message, f"Expected {expected!r}, got {actual!r}.")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def assert_not_equal(actual: str, expected: str, *, message: str | None = None) -> None:
|
|
48
|
+
"""Assert *actual* does not equal *expected*."""
|
|
49
|
+
passed = actual != expected
|
|
50
|
+
_emit("assert_not_equal", passed, expected=expected, actual=actual)
|
|
51
|
+
if not passed:
|
|
52
|
+
_fail(message, f"Expected value to differ from {expected!r}, got {actual!r}.")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def assert_equal_ignore_case(actual: str, expected: str, *, message: str | None = None) -> None:
|
|
56
|
+
"""Assert *actual* equals *expected*, ignoring case."""
|
|
57
|
+
passed = actual.lower() == expected.lower()
|
|
58
|
+
_emit("assert_equal_ignore_case", passed, expected=expected, actual=actual)
|
|
59
|
+
if not passed:
|
|
60
|
+
_fail(message, f"Expected {expected!r} (case-insensitive), got {actual!r}.")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def assert_contains(actual: str, expected_substring: str, *, message: str | None = None) -> None:
|
|
64
|
+
"""Assert *actual* contains *expected_substring*."""
|
|
65
|
+
passed = expected_substring in actual
|
|
66
|
+
_emit("assert_contains", passed, expected_substring=expected_substring, actual=actual)
|
|
67
|
+
if not passed:
|
|
68
|
+
_fail(message, f"Expected {actual!r} to contain {expected_substring!r}.")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def assert_matches(actual: str, pattern: str, *, message: str | None = None) -> None:
|
|
72
|
+
"""Assert *actual* matches the regex *pattern*."""
|
|
73
|
+
passed = re.search(pattern, actual) is not None
|
|
74
|
+
_emit("assert_matches", passed, pattern=pattern, actual=actual)
|
|
75
|
+
if not passed:
|
|
76
|
+
_fail(message, f"Expected {actual!r} to match /{pattern}/.")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def assert_is_empty(actual: str, *, message: str | None = None) -> None:
|
|
80
|
+
"""Assert *actual* is an empty string."""
|
|
81
|
+
passed = actual == ""
|
|
82
|
+
_emit("assert_is_empty", passed, actual=actual)
|
|
83
|
+
if not passed:
|
|
84
|
+
_fail(message, f"Expected empty string, got {actual!r}.")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def assert_is_not_empty(actual: str, *, message: str | None = None) -> None:
|
|
88
|
+
"""Assert *actual* is a non-empty string."""
|
|
89
|
+
passed = actual != ""
|
|
90
|
+
_emit("assert_is_not_empty", passed, actual=actual)
|
|
91
|
+
if not passed:
|
|
92
|
+
_fail(message, "Expected non-empty string, got empty string.")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def assert_matches_schema(
|
|
96
|
+
actual: object, schema: dict[str, Any], *, message: str | None = None
|
|
97
|
+
) -> None:
|
|
98
|
+
"""Assert *actual* conforms to the given JSON Schema, collecting all errors."""
|
|
99
|
+
import jsonschema
|
|
100
|
+
|
|
101
|
+
validator = jsonschema.Draft7Validator(schema)
|
|
102
|
+
errors = sorted(validator.iter_errors(actual), key=lambda e: list(e.path))
|
|
103
|
+
passed = not errors
|
|
104
|
+
error_messages = [e.message for e in errors]
|
|
105
|
+
_emit(
|
|
106
|
+
"assert_matches_schema",
|
|
107
|
+
passed,
|
|
108
|
+
errors=error_messages,
|
|
109
|
+
schema=json.dumps(schema, indent=2, default=str),
|
|
110
|
+
actual=json.dumps(actual, indent=2, default=str),
|
|
111
|
+
)
|
|
112
|
+
if not passed:
|
|
113
|
+
joined = "; ".join(error_messages)
|
|
114
|
+
_fail(message, f"Schema validation failed with {len(errors)} error(s): {joined}")
|