pytest-api-core 1.0.1__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.1/LICENSE +21 -0
- pytest_api_core-1.0.1/MANIFEST.in +4 -0
- pytest_api_core-1.0.1/PKG-INFO +314 -0
- pytest_api_core-1.0.1/README.md +254 -0
- pytest_api_core-1.0.1/pyproject.toml +72 -0
- pytest_api_core-1.0.1/setup.cfg +4 -0
- pytest_api_core-1.0.1/src/pytest_api_core/__init__.py +20 -0
- pytest_api_core-1.0.1/src/pytest_api_core/assertions/__init__.py +3 -0
- pytest_api_core-1.0.1/src/pytest_api_core/assertions/response_assertions.py +350 -0
- pytest_api_core-1.0.1/src/pytest_api_core/auth/__init__.py +8 -0
- pytest_api_core-1.0.1/src/pytest_api_core/auth/auth_handlers.py +156 -0
- pytest_api_core-1.0.1/src/pytest_api_core/client/__init__.py +4 -0
- pytest_api_core-1.0.1/src/pytest_api_core/client/api_client.py +183 -0
- pytest_api_core-1.0.1/src/pytest_api_core/client/api_response.py +101 -0
- pytest_api_core-1.0.1/src/pytest_api_core/config/__init__.py +3 -0
- pytest_api_core-1.0.1/src/pytest_api_core/config/base_settings.py +54 -0
- pytest_api_core-1.0.1/src/pytest_api_core/config/config_manager.py +156 -0
- pytest_api_core-1.0.1/src/pytest_api_core/config/env_loader.py +110 -0
- pytest_api_core-1.0.1/src/pytest_api_core/fixtures/__init__.py +9 -0
- pytest_api_core-1.0.1/src/pytest_api_core/fixtures/api_fixtures.py +133 -0
- pytest_api_core-1.0.1/src/pytest_api_core/plugin.py +125 -0
- pytest_api_core-1.0.1/src/pytest_api_core/reporters/__init__.py +3 -0
- pytest_api_core-1.0.1/src/pytest_api_core/reporters/html_reporter.py +245 -0
- pytest_api_core-1.0.1/src/pytest_api_core/reporters/templates/report.html +469 -0
- pytest_api_core-1.0.1/src/pytest_api_core.egg-info/PKG-INFO +314 -0
- pytest_api_core-1.0.1/src/pytest_api_core.egg-info/SOURCES.txt +30 -0
- pytest_api_core-1.0.1/src/pytest_api_core.egg-info/dependency_links.txt +1 -0
- pytest_api_core-1.0.1/src/pytest_api_core.egg-info/entry_points.txt +2 -0
- pytest_api_core-1.0.1/src/pytest_api_core.egg-info/requires.txt +17 -0
- pytest_api_core-1.0.1/src/pytest_api_core.egg-info/top_level.txt +1 -0
- pytest_api_core-1.0.1/tests/test_sample_api.py +150 -0
- pytest_api_core-1.0.1/tests/test_unit.py +187 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Subodh Singh
|
|
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,314 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-api-core
|
|
3
|
+
Version: 1.0.1
|
|
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: Repository, https://github.com/your-org/pytest-api-core-framework
|
|
29
|
+
Project-URL: Documentation, https://your-org.artifactory.com/docs/pytest-api-core
|
|
30
|
+
Keywords: pytest,api,testing,automation,html-report
|
|
31
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
32
|
+
Classifier: Framework :: Pytest
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: Topic :: Software Development :: Testing
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Operating System :: OS Independent
|
|
41
|
+
Requires-Python: >=3.9
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
License-File: LICENSE
|
|
44
|
+
Requires-Dist: pytest>=7.4
|
|
45
|
+
Requires-Dist: requests>=2.31
|
|
46
|
+
Requires-Dist: Jinja2>=3.1
|
|
47
|
+
Requires-Dist: urllib3>=2.0
|
|
48
|
+
Requires-Dist: jsonschema>=4.20
|
|
49
|
+
Provides-Extra: dotenv
|
|
50
|
+
Requires-Dist: python-dotenv>=1.0; extra == "dotenv"
|
|
51
|
+
Provides-Extra: dev
|
|
52
|
+
Requires-Dist: pytest-mock>=3.12; extra == "dev"
|
|
53
|
+
Requires-Dist: responses>=0.25; extra == "dev"
|
|
54
|
+
Requires-Dist: black>=24.0; extra == "dev"
|
|
55
|
+
Requires-Dist: isort>=5.13; extra == "dev"
|
|
56
|
+
Requires-Dist: mypy>=1.8; extra == "dev"
|
|
57
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
58
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
59
|
+
Dynamic: license-file
|
|
60
|
+
|
|
61
|
+
# pytest-api-core
|
|
62
|
+
|
|
63
|
+
> A reusable pytest API automation framework with fluent assertions, built-in auth strategies, environment-aware configuration, and a beautiful custom HTML report — packaged for Artifactory distribution.
|
|
64
|
+
|
|
65
|
+
## Features
|
|
66
|
+
|
|
67
|
+
- **`APIClient`** — `requests.Session` wrapper with retry, timeout, and structured logging
|
|
68
|
+
- **Fluent response assertions** — `assert_that(response).status_is(200).json_path("$.id").equals(1)`
|
|
69
|
+
- **Auth strategies** — Bearer token, Basic, API Key (header/query), OAuth2 client credentials
|
|
70
|
+
- **Environment config** — Python `settings.py` classes + `.env` file + `ENV_VAR` overrides via `api_config` fixture
|
|
71
|
+
- **Custom HTML report** — self-contained report with charts, filterable table, captured logs, and request/response details
|
|
72
|
+
- **Auto-registered pytest fixtures** — zero boilerplate in consuming projects
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Installation
|
|
77
|
+
|
|
78
|
+
### From private PyPI
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pip install pytest-api-core \
|
|
82
|
+
--index-url https://pypi.example.com/simple/
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
With `.env` support (optional but recommended):
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install "pytest-api-core[dotenv]"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Or add to `requirements.txt` / `pyproject.toml`:
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
pytest-api-core==1.0.0
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Quick Start
|
|
100
|
+
|
|
101
|
+
### 1. Create environment settings
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
# config/settings.py
|
|
105
|
+
import os
|
|
106
|
+
from pytest_api_core.config.base_settings import BaseSettings
|
|
107
|
+
|
|
108
|
+
class DevSettings(BaseSettings):
|
|
109
|
+
base_url = "https://api.dev.example.com"
|
|
110
|
+
timeout = 30
|
|
111
|
+
verify_ssl = True
|
|
112
|
+
headers = {"Accept": "application/json", "Content-Type": "application/json"}
|
|
113
|
+
|
|
114
|
+
class StagingSettings(DevSettings):
|
|
115
|
+
base_url = os.environ.get("API_BASE_URL", "https://api.staging.example.com")
|
|
116
|
+
timeout = 60
|
|
117
|
+
|
|
118
|
+
ENVIRONMENTS = {
|
|
119
|
+
"dev": DevSettings,
|
|
120
|
+
"staging": StagingSettings,
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 2. Store secrets in `.env` (never commit this file)
|
|
125
|
+
|
|
126
|
+
```ini
|
|
127
|
+
# .env
|
|
128
|
+
BEARER_TOKEN=eyJhbGciOiJIUzI1NiIs...
|
|
129
|
+
API_KEY=super-secret-key
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 3. Configure pytest.ini
|
|
133
|
+
|
|
134
|
+
```ini
|
|
135
|
+
[pytest]
|
|
136
|
+
|
|
137
|
+
# ── Framework ────────────────────────────────────────────────────────────────
|
|
138
|
+
api_env = dev
|
|
139
|
+
api_settings_module = config.settings
|
|
140
|
+
api_dotenv_file = .env
|
|
141
|
+
|
|
142
|
+
# ── Report ───────────────────────────────────────────────────────────────────
|
|
143
|
+
addopts = --api-html-report=reports/{env}/report_{timestamp}.html -v
|
|
144
|
+
api_html_theme = dark # or "light"
|
|
145
|
+
api_html_title = API Test Report # browser tab title
|
|
146
|
+
api_html_header = API Test Report # page header text
|
|
147
|
+
|
|
148
|
+
# ── Logging ──────────────────────────────────────────────────────────────────
|
|
149
|
+
api_log_level = INFO
|
|
150
|
+
log_cli = true
|
|
151
|
+
log_cli_level = INFO
|
|
152
|
+
log_cli_format = %(asctime)s [%(levelname)-8s] %(name)s: %(message)s
|
|
153
|
+
log_cli_date_format = %H:%M:%S
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 4. Write tests
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
# tests/test_posts.py
|
|
160
|
+
from pytest_api_core.assertions import assert_that
|
|
161
|
+
|
|
162
|
+
def test_get_post(api_client):
|
|
163
|
+
response = api_client.get("/posts/1")
|
|
164
|
+
assert_that(response).status_is(200).json_path("$.id").equals(1)
|
|
165
|
+
|
|
166
|
+
def test_create_post(api_client):
|
|
167
|
+
payload = {"title": "foo", "body": "bar", "userId": 1}
|
|
168
|
+
response = api_client.post("/posts", json=payload)
|
|
169
|
+
assert_that(response).status_is(201).has_key("id")
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### 5. Run with HTML report
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
pytest tests/ --api-env=staging
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The report is written to `reports/staging/report_<timestamp>.html`.
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## Configuration
|
|
183
|
+
|
|
184
|
+
### Resolution order (highest → lowest priority)
|
|
185
|
+
|
|
186
|
+
| Priority | Source |
|
|
187
|
+
|---|---|
|
|
188
|
+
| 1 | Shell / CI environment variables (`API_BASE_URL`, `API_TOKEN`, …) |
|
|
189
|
+
| 2 | `--api-base-url` CLI flag |
|
|
190
|
+
| 3 | `ENVIRONMENTS[env]` class in `settings_module` |
|
|
191
|
+
| 4 | Built-in defaults (`http://localhost`, timeout 30 s, …) |
|
|
192
|
+
|
|
193
|
+
### pytest.ini options
|
|
194
|
+
|
|
195
|
+
| Option | Description | Default |
|
|
196
|
+
|---|---|---|
|
|
197
|
+
| `api_env` | Active environment name | `dev` |
|
|
198
|
+
| `api_settings_module` | Dotted path to settings module | — |
|
|
199
|
+
| `api_dotenv_file` | Path to `.env` file | `.env` |
|
|
200
|
+
| `api_log_level` | Log level for framework internals | `WARNING` |
|
|
201
|
+
| `api_html_report` | Output path for HTML report (supports `{env}`, `{timestamp}`) | — |
|
|
202
|
+
| `api_html_theme` | Report theme: `light` or `dark` | `dark` |
|
|
203
|
+
| `api_html_title` | Browser tab title text | `API Test Report` |
|
|
204
|
+
| `api_html_header` | Page header text | `API Test Report` |
|
|
205
|
+
|
|
206
|
+
### CLI flags
|
|
207
|
+
|
|
208
|
+
| Flag | Description |
|
|
209
|
+
|---|---|
|
|
210
|
+
| `--api-env` | Override active environment |
|
|
211
|
+
| `--api-base-url` | Override `base_url` |
|
|
212
|
+
| `--api-log-level` | Override framework log level |
|
|
213
|
+
| `--api-html-report` | Override HTML report path |
|
|
214
|
+
|
|
215
|
+
### Environment variables
|
|
216
|
+
|
|
217
|
+
| Variable | Purpose |
|
|
218
|
+
|---|---|
|
|
219
|
+
| `API_BASE_URL` | Override `base_url` |
|
|
220
|
+
| `API_TOKEN` | Inject Bearer token (auto-applied by `api_client`) |
|
|
221
|
+
| `API_ENV` | Select environment |
|
|
222
|
+
| `API_TIMEOUT` | Override request timeout |
|
|
223
|
+
| `API_VERIFY_SSL` | Override SSL verification |
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Fixtures
|
|
228
|
+
|
|
229
|
+
| Fixture | Scope | Description |
|
|
230
|
+
|---|---|---|
|
|
231
|
+
| `api_config` | session | Resolved config dict for the active env |
|
|
232
|
+
| `api_client` | session | Configured `APIClient` instance |
|
|
233
|
+
| `api_bearer_auth` | function | `BearerAuth` built from `API_TOKEN` env var |
|
|
234
|
+
| `api_basic_auth` | function | `BasicAuth` built from `API_USERNAME` / `API_PASSWORD` |
|
|
235
|
+
| `api_key_auth` | function | `APIKeyAuth` built from `API_KEY_NAME` / `API_KEY_VALUE` |
|
|
236
|
+
|
|
237
|
+
### Overriding `api_client` per project
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
# tests/conftest.py
|
|
241
|
+
import pytest
|
|
242
|
+
from pytest_api_core.auth.auth_handlers import BearerAuth
|
|
243
|
+
from pytest_api_core.client.api_client import APIClient
|
|
244
|
+
from pytest_api_core.config.env_loader import get_env
|
|
245
|
+
|
|
246
|
+
@pytest.fixture(scope="session")
|
|
247
|
+
def api_client(api_config):
|
|
248
|
+
token = get_env("BEARER_TOKEN", required=True)
|
|
249
|
+
client = APIClient(
|
|
250
|
+
base_url=api_config["base_url"],
|
|
251
|
+
auth=BearerAuth(token),
|
|
252
|
+
timeout=api_config.get("timeout", 30),
|
|
253
|
+
verify_ssl=api_config.get("verify_ssl", True),
|
|
254
|
+
default_headers=api_config.get("headers"),
|
|
255
|
+
)
|
|
256
|
+
yield client
|
|
257
|
+
client.close()
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## Publishing
|
|
263
|
+
|
|
264
|
+
### Via GitHub Actions (Recommended)
|
|
265
|
+
|
|
266
|
+
Use the **Publish to Public PyPI** workflow for automated releases:
|
|
267
|
+
|
|
268
|
+
1. Go to **Actions** → **Publish to Public PyPI**
|
|
269
|
+
2. Click **Run workflow**
|
|
270
|
+
3. Enter version (e.g., `1.0.1`) and enable/disable tests
|
|
271
|
+
4. Workflow will build, test, and publish to PyPI
|
|
272
|
+
|
|
273
|
+
See [docs/PUBLISHING.md](docs/PUBLISHING.md) for detailed setup instructions.
|
|
274
|
+
|
|
275
|
+
### Manual Publishing
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
# Build
|
|
279
|
+
python -m build
|
|
280
|
+
|
|
281
|
+
# Upload to PyPI
|
|
282
|
+
twine upload dist/*
|
|
283
|
+
|
|
284
|
+
# Or upload to private registry
|
|
285
|
+
twine upload \
|
|
286
|
+
--repository-url https://pypi.example.com \
|
|
287
|
+
-u <user> -p <token> \
|
|
288
|
+
dist/*
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Project Layout
|
|
294
|
+
|
|
295
|
+
```
|
|
296
|
+
src/
|
|
297
|
+
└── pytest_api_core/
|
|
298
|
+
├── plugin.py # pytest entry-point
|
|
299
|
+
├── client/ # HTTP client + response wrapper
|
|
300
|
+
├── auth/ # Auth strategy classes
|
|
301
|
+
├── config/ # Config manager, BaseSettings, env_loader
|
|
302
|
+
├── fixtures/ # Auto-registered pytest fixtures
|
|
303
|
+
├── assertions/ # Fluent response assertion API
|
|
304
|
+
└── reporters/ # Custom HTML report plugin
|
|
305
|
+
config/
|
|
306
|
+
└── settings.py # Project environment settings (not shipped in wheel)
|
|
307
|
+
tests/ # Package self-tests
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
## License
|
|
313
|
+
|
|
314
|
+
See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# pytest-api-core
|
|
2
|
+
|
|
3
|
+
> A reusable pytest API automation framework with fluent assertions, built-in auth strategies, environment-aware configuration, and a beautiful custom HTML report — packaged for Artifactory distribution.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **`APIClient`** — `requests.Session` wrapper with retry, timeout, and structured logging
|
|
8
|
+
- **Fluent response assertions** — `assert_that(response).status_is(200).json_path("$.id").equals(1)`
|
|
9
|
+
- **Auth strategies** — Bearer token, Basic, API Key (header/query), OAuth2 client credentials
|
|
10
|
+
- **Environment config** — Python `settings.py` classes + `.env` file + `ENV_VAR` overrides via `api_config` fixture
|
|
11
|
+
- **Custom HTML report** — self-contained report with charts, filterable table, captured logs, and request/response details
|
|
12
|
+
- **Auto-registered pytest fixtures** — zero boilerplate in consuming projects
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
### From private PyPI
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install pytest-api-core \
|
|
22
|
+
--index-url https://pypi.example.com/simple/
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
With `.env` support (optional but recommended):
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install "pytest-api-core[dotenv]"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or add to `requirements.txt` / `pyproject.toml`:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
pytest-api-core==1.0.0
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Quick Start
|
|
40
|
+
|
|
41
|
+
### 1. Create environment settings
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
# config/settings.py
|
|
45
|
+
import os
|
|
46
|
+
from pytest_api_core.config.base_settings import BaseSettings
|
|
47
|
+
|
|
48
|
+
class DevSettings(BaseSettings):
|
|
49
|
+
base_url = "https://api.dev.example.com"
|
|
50
|
+
timeout = 30
|
|
51
|
+
verify_ssl = True
|
|
52
|
+
headers = {"Accept": "application/json", "Content-Type": "application/json"}
|
|
53
|
+
|
|
54
|
+
class StagingSettings(DevSettings):
|
|
55
|
+
base_url = os.environ.get("API_BASE_URL", "https://api.staging.example.com")
|
|
56
|
+
timeout = 60
|
|
57
|
+
|
|
58
|
+
ENVIRONMENTS = {
|
|
59
|
+
"dev": DevSettings,
|
|
60
|
+
"staging": StagingSettings,
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. Store secrets in `.env` (never commit this file)
|
|
65
|
+
|
|
66
|
+
```ini
|
|
67
|
+
# .env
|
|
68
|
+
BEARER_TOKEN=eyJhbGciOiJIUzI1NiIs...
|
|
69
|
+
API_KEY=super-secret-key
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Configure pytest.ini
|
|
73
|
+
|
|
74
|
+
```ini
|
|
75
|
+
[pytest]
|
|
76
|
+
|
|
77
|
+
# ── Framework ────────────────────────────────────────────────────────────────
|
|
78
|
+
api_env = dev
|
|
79
|
+
api_settings_module = config.settings
|
|
80
|
+
api_dotenv_file = .env
|
|
81
|
+
|
|
82
|
+
# ── Report ───────────────────────────────────────────────────────────────────
|
|
83
|
+
addopts = --api-html-report=reports/{env}/report_{timestamp}.html -v
|
|
84
|
+
api_html_theme = dark # or "light"
|
|
85
|
+
api_html_title = API Test Report # browser tab title
|
|
86
|
+
api_html_header = API Test Report # page header text
|
|
87
|
+
|
|
88
|
+
# ── Logging ──────────────────────────────────────────────────────────────────
|
|
89
|
+
api_log_level = INFO
|
|
90
|
+
log_cli = true
|
|
91
|
+
log_cli_level = INFO
|
|
92
|
+
log_cli_format = %(asctime)s [%(levelname)-8s] %(name)s: %(message)s
|
|
93
|
+
log_cli_date_format = %H:%M:%S
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 4. Write tests
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
# tests/test_posts.py
|
|
100
|
+
from pytest_api_core.assertions import assert_that
|
|
101
|
+
|
|
102
|
+
def test_get_post(api_client):
|
|
103
|
+
response = api_client.get("/posts/1")
|
|
104
|
+
assert_that(response).status_is(200).json_path("$.id").equals(1)
|
|
105
|
+
|
|
106
|
+
def test_create_post(api_client):
|
|
107
|
+
payload = {"title": "foo", "body": "bar", "userId": 1}
|
|
108
|
+
response = api_client.post("/posts", json=payload)
|
|
109
|
+
assert_that(response).status_is(201).has_key("id")
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 5. Run with HTML report
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
pytest tests/ --api-env=staging
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The report is written to `reports/staging/report_<timestamp>.html`.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Configuration
|
|
123
|
+
|
|
124
|
+
### Resolution order (highest → lowest priority)
|
|
125
|
+
|
|
126
|
+
| Priority | Source |
|
|
127
|
+
|---|---|
|
|
128
|
+
| 1 | Shell / CI environment variables (`API_BASE_URL`, `API_TOKEN`, …) |
|
|
129
|
+
| 2 | `--api-base-url` CLI flag |
|
|
130
|
+
| 3 | `ENVIRONMENTS[env]` class in `settings_module` |
|
|
131
|
+
| 4 | Built-in defaults (`http://localhost`, timeout 30 s, …) |
|
|
132
|
+
|
|
133
|
+
### pytest.ini options
|
|
134
|
+
|
|
135
|
+
| Option | Description | Default |
|
|
136
|
+
|---|---|---|
|
|
137
|
+
| `api_env` | Active environment name | `dev` |
|
|
138
|
+
| `api_settings_module` | Dotted path to settings module | — |
|
|
139
|
+
| `api_dotenv_file` | Path to `.env` file | `.env` |
|
|
140
|
+
| `api_log_level` | Log level for framework internals | `WARNING` |
|
|
141
|
+
| `api_html_report` | Output path for HTML report (supports `{env}`, `{timestamp}`) | — |
|
|
142
|
+
| `api_html_theme` | Report theme: `light` or `dark` | `dark` |
|
|
143
|
+
| `api_html_title` | Browser tab title text | `API Test Report` |
|
|
144
|
+
| `api_html_header` | Page header text | `API Test Report` |
|
|
145
|
+
|
|
146
|
+
### CLI flags
|
|
147
|
+
|
|
148
|
+
| Flag | Description |
|
|
149
|
+
|---|---|
|
|
150
|
+
| `--api-env` | Override active environment |
|
|
151
|
+
| `--api-base-url` | Override `base_url` |
|
|
152
|
+
| `--api-log-level` | Override framework log level |
|
|
153
|
+
| `--api-html-report` | Override HTML report path |
|
|
154
|
+
|
|
155
|
+
### Environment variables
|
|
156
|
+
|
|
157
|
+
| Variable | Purpose |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `API_BASE_URL` | Override `base_url` |
|
|
160
|
+
| `API_TOKEN` | Inject Bearer token (auto-applied by `api_client`) |
|
|
161
|
+
| `API_ENV` | Select environment |
|
|
162
|
+
| `API_TIMEOUT` | Override request timeout |
|
|
163
|
+
| `API_VERIFY_SSL` | Override SSL verification |
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Fixtures
|
|
168
|
+
|
|
169
|
+
| Fixture | Scope | Description |
|
|
170
|
+
|---|---|---|
|
|
171
|
+
| `api_config` | session | Resolved config dict for the active env |
|
|
172
|
+
| `api_client` | session | Configured `APIClient` instance |
|
|
173
|
+
| `api_bearer_auth` | function | `BearerAuth` built from `API_TOKEN` env var |
|
|
174
|
+
| `api_basic_auth` | function | `BasicAuth` built from `API_USERNAME` / `API_PASSWORD` |
|
|
175
|
+
| `api_key_auth` | function | `APIKeyAuth` built from `API_KEY_NAME` / `API_KEY_VALUE` |
|
|
176
|
+
|
|
177
|
+
### Overriding `api_client` per project
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
# tests/conftest.py
|
|
181
|
+
import pytest
|
|
182
|
+
from pytest_api_core.auth.auth_handlers import BearerAuth
|
|
183
|
+
from pytest_api_core.client.api_client import APIClient
|
|
184
|
+
from pytest_api_core.config.env_loader import get_env
|
|
185
|
+
|
|
186
|
+
@pytest.fixture(scope="session")
|
|
187
|
+
def api_client(api_config):
|
|
188
|
+
token = get_env("BEARER_TOKEN", required=True)
|
|
189
|
+
client = APIClient(
|
|
190
|
+
base_url=api_config["base_url"],
|
|
191
|
+
auth=BearerAuth(token),
|
|
192
|
+
timeout=api_config.get("timeout", 30),
|
|
193
|
+
verify_ssl=api_config.get("verify_ssl", True),
|
|
194
|
+
default_headers=api_config.get("headers"),
|
|
195
|
+
)
|
|
196
|
+
yield client
|
|
197
|
+
client.close()
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## Publishing
|
|
203
|
+
|
|
204
|
+
### Via GitHub Actions (Recommended)
|
|
205
|
+
|
|
206
|
+
Use the **Publish to Public PyPI** workflow for automated releases:
|
|
207
|
+
|
|
208
|
+
1. Go to **Actions** → **Publish to Public PyPI**
|
|
209
|
+
2. Click **Run workflow**
|
|
210
|
+
3. Enter version (e.g., `1.0.1`) and enable/disable tests
|
|
211
|
+
4. Workflow will build, test, and publish to PyPI
|
|
212
|
+
|
|
213
|
+
See [docs/PUBLISHING.md](docs/PUBLISHING.md) for detailed setup instructions.
|
|
214
|
+
|
|
215
|
+
### Manual Publishing
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
# Build
|
|
219
|
+
python -m build
|
|
220
|
+
|
|
221
|
+
# Upload to PyPI
|
|
222
|
+
twine upload dist/*
|
|
223
|
+
|
|
224
|
+
# Or upload to private registry
|
|
225
|
+
twine upload \
|
|
226
|
+
--repository-url https://pypi.example.com \
|
|
227
|
+
-u <user> -p <token> \
|
|
228
|
+
dist/*
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## Project Layout
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
src/
|
|
237
|
+
└── pytest_api_core/
|
|
238
|
+
├── plugin.py # pytest entry-point
|
|
239
|
+
├── client/ # HTTP client + response wrapper
|
|
240
|
+
├── auth/ # Auth strategy classes
|
|
241
|
+
├── config/ # Config manager, BaseSettings, env_loader
|
|
242
|
+
├── fixtures/ # Auto-registered pytest fixtures
|
|
243
|
+
├── assertions/ # Fluent response assertion API
|
|
244
|
+
└── reporters/ # Custom HTML report plugin
|
|
245
|
+
config/
|
|
246
|
+
└── settings.py # Project environment settings (not shipped in wheel)
|
|
247
|
+
tests/ # Package self-tests
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pytest-api-core"
|
|
7
|
+
version = "1.0.1"
|
|
8
|
+
description = "Pytest API automation framework with custom HTML reporting, reusable fixtures, and fluent assertions"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
authors = [{ name = "QA Platform Team" }]
|
|
12
|
+
keywords = ["pytest", "api", "testing", "automation", "html-report"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 5 - Production/Stable",
|
|
15
|
+
"Framework :: Pytest",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Topic :: Software Development :: Testing",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.9",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
]
|
|
25
|
+
requires-python = ">=3.9"
|
|
26
|
+
dependencies = [
|
|
27
|
+
"pytest>=7.4",
|
|
28
|
+
"requests>=2.31",
|
|
29
|
+
"Jinja2>=3.1",
|
|
30
|
+
"urllib3>=2.0",
|
|
31
|
+
"jsonschema>=4.20",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
dotenv = [
|
|
36
|
+
"python-dotenv>=1.0",
|
|
37
|
+
]
|
|
38
|
+
dev = [
|
|
39
|
+
"pytest-mock>=3.12",
|
|
40
|
+
"responses>=0.25",
|
|
41
|
+
"black>=24.0",
|
|
42
|
+
"isort>=5.13",
|
|
43
|
+
"mypy>=1.8",
|
|
44
|
+
"twine>=5.0",
|
|
45
|
+
"build>=1.0",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[project.entry-points."pytest11"]
|
|
49
|
+
pytest-api-core = "pytest_api_core.plugin"
|
|
50
|
+
|
|
51
|
+
[project.urls]
|
|
52
|
+
Repository = "https://github.com/your-org/pytest-api-core-framework"
|
|
53
|
+
Documentation = "https://your-org.artifactory.com/docs/pytest-api-core"
|
|
54
|
+
|
|
55
|
+
[tool.setuptools.packages.find]
|
|
56
|
+
where = ["src"]
|
|
57
|
+
|
|
58
|
+
[tool.setuptools.package-data]
|
|
59
|
+
pytest_api_core = ["reporters/templates/*.html"]
|
|
60
|
+
|
|
61
|
+
[tool.black]
|
|
62
|
+
line-length = 100
|
|
63
|
+
target-version = ["py39"]
|
|
64
|
+
|
|
65
|
+
[tool.isort]
|
|
66
|
+
profile = "black"
|
|
67
|
+
line_length = 100
|
|
68
|
+
|
|
69
|
+
[tool.mypy]
|
|
70
|
+
python_version = "3.9"
|
|
71
|
+
strict = true
|
|
72
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,20 @@
|
|
|
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.client.api_client import APIClient
|
|
7
|
+
from pytest_api_core.client.api_response import APIResponse
|
|
8
|
+
from pytest_api_core.assertions.response_assertions import assert_that
|
|
9
|
+
from pytest_api_core.auth.auth_handlers import BearerAuth, BasicAuth, APIKeyAuth, OAuth2ClientCredentials
|
|
10
|
+
|
|
11
|
+
__version__ = "1.0.0"
|
|
12
|
+
__all__ = [
|
|
13
|
+
"APIClient",
|
|
14
|
+
"APIResponse",
|
|
15
|
+
"assert_that",
|
|
16
|
+
"BearerAuth",
|
|
17
|
+
"BasicAuth",
|
|
18
|
+
"APIKeyAuth",
|
|
19
|
+
"OAuth2ClientCredentials",
|
|
20
|
+
]
|