pytest-api-core 1.0.10__tar.gz → 1.1.0__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.10/src/pytest_api_core.egg-info → pytest_api_core-1.1.0}/PKG-INFO +8 -5
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/README.md +7 -4
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/pyproject.toml +1 -1
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/__init__.py +3 -1
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/assertions/__init__.py +2 -0
- pytest_api_core-1.1.0/src/pytest_api_core/assertions/assert_utils.py +342 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/assertions/response_assertions.py +293 -24
- pytest_api_core-1.1.0/src/pytest_api_core/assertions/soft.py +64 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/client/api_client.py +71 -21
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/client/api_response.py +13 -10
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/reporters/html_reporter.py +96 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/reporters/templates/report.html +109 -5
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0/src/pytest_api_core.egg-info}/PKG-INFO +8 -5
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core.egg-info/SOURCES.txt +1 -0
- pytest_api_core-1.0.10/src/pytest_api_core/assertions/assert_utils.py +0 -149
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/LICENSE +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/MANIFEST.in +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/setup.cfg +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/auth/__init__.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/auth/auth_handlers.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/client/__init__.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/config/__init__.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/config/base_settings.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/config/config_manager.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/config/env_loader.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/fixtures/__init__.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/fixtures/api_fixtures.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/plugin.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/reporters/__init__.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/security/__init__.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core/security/redaction.py +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core.egg-info/dependency_links.txt +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core.egg-info/entry_points.txt +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core.egg-info/requires.txt +0 -0
- {pytest_api_core-1.0.10 → pytest_api_core-1.1.0}/src/pytest_api_core.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytest-api-core
|
|
3
|
-
Version: 1.0
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: Pytest API automation framework with custom HTML reporting, reusable fixtures, and fluent assertions
|
|
5
5
|
Author: QA Platform Team
|
|
6
6
|
License: MIT License
|
|
@@ -75,7 +75,7 @@ Dynamic: license-file
|
|
|
75
75
|
## Features
|
|
76
76
|
|
|
77
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)`
|
|
78
|
+
- **Fluent assertions** — `assert_that(response).status_is(200).json_path("$.id").equals(1)`, plus `json_path` matchers like `is_type`, `contains`, `is_in`, `has_length`, `greater_than`/`less_than`/`between` and `has_path()`
|
|
79
79
|
(`json_path()` supports a small dot/bracket-index subset, not full JSONPath — see [docs/QUICKSTART.md](docs/QUICKSTART.md#json_path-syntax);
|
|
80
80
|
`response_time_under()` measures the whole request including any retries, not just the final attempt)
|
|
81
81
|
- **Auth strategies** — Bearer token, Basic, API Key (header/query), OAuth2 client credentials
|
|
@@ -83,7 +83,10 @@ Dynamic: license-file
|
|
|
83
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
84
|
- **Configurable retry policy** — max attempts, backoff factor, and retryable methods, overridable per environment (see below)
|
|
85
85
|
- **Environment config** — Python `settings.py` classes + `.env` file + env var overrides
|
|
86
|
-
- **
|
|
86
|
+
- **Soft assertions** — `with soft_assertions():` runs every check and fails once with all the failures listed — see [docs/QUICKSTART.md](docs/QUICKSTART.md#soft-assertions)
|
|
87
|
+
- **Custom HTML report** — self-contained file with charts, filterable table, and request/response details,
|
|
88
|
+
a "Copy cURL" button per request (secrets stay redacted), an ERROR banner for timeouts / connection errors,
|
|
89
|
+
and a per-endpoint latency table (min/p50/p95/max)
|
|
87
90
|
- **Secret redaction** — tokens, passwords, API keys and cookies are masked in logs, assertion messages, and the HTML report (see [Secret redaction](#secret-redaction))
|
|
88
91
|
- **Auto-registered fixtures** — zero boilerplate in consuming projects
|
|
89
92
|
|
|
@@ -92,13 +95,13 @@ Dynamic: license-file
|
|
|
92
95
|
## Installation
|
|
93
96
|
|
|
94
97
|
```bash
|
|
95
|
-
pip install pytest-api-core==1.0
|
|
98
|
+
pip install pytest-api-core==1.1.0
|
|
96
99
|
```
|
|
97
100
|
|
|
98
101
|
With `.env` file support (recommended):
|
|
99
102
|
|
|
100
103
|
```bash
|
|
101
|
-
pip install "pytest-api-core[dotenv]==1.0
|
|
104
|
+
pip install "pytest-api-core[dotenv]==1.1.0"
|
|
102
105
|
```
|
|
103
106
|
|
|
104
107
|
---
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
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)`
|
|
12
|
+
- **Fluent assertions** — `assert_that(response).status_is(200).json_path("$.id").equals(1)`, plus `json_path` matchers like `is_type`, `contains`, `is_in`, `has_length`, `greater_than`/`less_than`/`between` and `has_path()`
|
|
13
13
|
(`json_path()` supports a small dot/bracket-index subset, not full JSONPath — see [docs/QUICKSTART.md](docs/QUICKSTART.md#json_path-syntax);
|
|
14
14
|
`response_time_under()` measures the whole request including any retries, not just the final attempt)
|
|
15
15
|
- **Auth strategies** — Bearer token, Basic, API Key (header/query), OAuth2 client credentials
|
|
@@ -17,7 +17,10 @@
|
|
|
17
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
18
|
- **Configurable retry policy** — max attempts, backoff factor, and retryable methods, overridable per environment (see below)
|
|
19
19
|
- **Environment config** — Python `settings.py` classes + `.env` file + env var overrides
|
|
20
|
-
- **
|
|
20
|
+
- **Soft assertions** — `with soft_assertions():` runs every check and fails once with all the failures listed — see [docs/QUICKSTART.md](docs/QUICKSTART.md#soft-assertions)
|
|
21
|
+
- **Custom HTML report** — self-contained file with charts, filterable table, and request/response details,
|
|
22
|
+
a "Copy cURL" button per request (secrets stay redacted), an ERROR banner for timeouts / connection errors,
|
|
23
|
+
and a per-endpoint latency table (min/p50/p95/max)
|
|
21
24
|
- **Secret redaction** — tokens, passwords, API keys and cookies are masked in logs, assertion messages, and the HTML report (see [Secret redaction](#secret-redaction))
|
|
22
25
|
- **Auto-registered fixtures** — zero boilerplate in consuming projects
|
|
23
26
|
|
|
@@ -26,13 +29,13 @@
|
|
|
26
29
|
## Installation
|
|
27
30
|
|
|
28
31
|
```bash
|
|
29
|
-
pip install pytest-api-core==1.0
|
|
32
|
+
pip install pytest-api-core==1.1.0
|
|
30
33
|
```
|
|
31
34
|
|
|
32
35
|
With `.env` file support (recommended):
|
|
33
36
|
|
|
34
37
|
```bash
|
|
35
|
-
pip install "pytest-api-core[dotenv]==1.0
|
|
38
|
+
pip install "pytest-api-core[dotenv]==1.1.0"
|
|
36
39
|
```
|
|
37
40
|
|
|
38
41
|
---
|
|
@@ -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.1.0"
|
|
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" }
|
|
@@ -13,6 +13,7 @@ from pytest_api_core.assertions.assert_utils import (
|
|
|
13
13
|
assert_not_equal,
|
|
14
14
|
)
|
|
15
15
|
from pytest_api_core.assertions.response_assertions import assert_that
|
|
16
|
+
from pytest_api_core.assertions.soft import soft_assertions
|
|
16
17
|
from pytest_api_core.auth.auth_handlers import (
|
|
17
18
|
APIKeyAuth,
|
|
18
19
|
BasicAuth,
|
|
@@ -23,11 +24,12 @@ from pytest_api_core.client.api_client import APIClient
|
|
|
23
24
|
from pytest_api_core.client.api_response import APIResponse
|
|
24
25
|
from pytest_api_core.security.redaction import add_sensitive_keys, register_secret
|
|
25
26
|
|
|
26
|
-
__version__ = "1.0
|
|
27
|
+
__version__ = "1.1.0"
|
|
27
28
|
__all__ = [
|
|
28
29
|
"APIClient",
|
|
29
30
|
"APIResponse",
|
|
30
31
|
"assert_that",
|
|
32
|
+
"soft_assertions",
|
|
31
33
|
"assert_equal",
|
|
32
34
|
"assert_not_equal",
|
|
33
35
|
"assert_equal_ignore_case",
|
|
@@ -9,10 +9,12 @@ from pytest_api_core.assertions.assert_utils import (
|
|
|
9
9
|
assert_not_equal,
|
|
10
10
|
)
|
|
11
11
|
from pytest_api_core.assertions.response_assertions import ResponseAssertions, assert_that
|
|
12
|
+
from pytest_api_core.assertions.soft import soft_assertions
|
|
12
13
|
|
|
13
14
|
__all__ = [
|
|
14
15
|
"ResponseAssertions",
|
|
15
16
|
"assert_that",
|
|
17
|
+
"soft_assertions",
|
|
16
18
|
"assert_equal",
|
|
17
19
|
"assert_not_equal",
|
|
18
20
|
"assert_equal_ignore_case",
|
|
@@ -0,0 +1,342 @@
|
|
|
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_equal(user["name"], "Alice", field="user.name") # field shown in report
|
|
10
|
+
assert_contains(actual, "substring")
|
|
11
|
+
assert_matches(actual, r"^\\d+$")
|
|
12
|
+
assert_matches_schema(actual, schema)
|
|
13
|
+
assert_is_not_empty(token, sensitive=True) # value masked in logs/report
|
|
14
|
+
|
|
15
|
+
Registered secrets (auth handler tokens, ``register_secret()``), Bearer
|
|
16
|
+
credentials and JWTs are always scrubbed from sentinels and failure messages.
|
|
17
|
+
Pass ``sensitive=True`` to mask the actual/expected values entirely.
|
|
18
|
+
|
|
19
|
+
Each assertion is labelled with a field name, shown next to the expected/actual
|
|
20
|
+
values in the HTML report and prefixed to the failure message. Pass
|
|
21
|
+
``field="..."`` to set it; otherwise it is taken from the source text of the
|
|
22
|
+
first argument (``assert_equal(body["name"], "Alice")`` → ``body["name"]``).
|
|
23
|
+
A literal first argument gets no field name. A sensitive field name (``token``,
|
|
24
|
+
``password``, …) masks the values automatically, as if ``sensitive=True`` had
|
|
25
|
+
been passed.
|
|
26
|
+
|
|
27
|
+
Unlike :class:`ResponseAssertions`, these are plain functions for comparing
|
|
28
|
+
arbitrary actual/expected strings — not tied to an ``APIResponse``.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from __future__ import annotations
|
|
32
|
+
|
|
33
|
+
import ast
|
|
34
|
+
import functools
|
|
35
|
+
import inspect
|
|
36
|
+
import json
|
|
37
|
+
import linecache
|
|
38
|
+
import logging
|
|
39
|
+
import re
|
|
40
|
+
from typing import Any
|
|
41
|
+
|
|
42
|
+
from pytest_api_core.assertions.soft import record, soft_active
|
|
43
|
+
from pytest_api_core.security.redaction import (
|
|
44
|
+
is_sensitive_key,
|
|
45
|
+
mask,
|
|
46
|
+
redact,
|
|
47
|
+
schema_error_message,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
_assert_log = logging.getLogger("pytest_api_core.assertions")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _emit(name: str, passed: bool, field: str | None, **detail: object) -> None:
|
|
54
|
+
"""Emit a machine-readable sentinel line captured by HTMLReporter (secrets redacted)."""
|
|
55
|
+
if field is not None:
|
|
56
|
+
detail = {"field": field, **detail}
|
|
57
|
+
_assert_log.debug(
|
|
58
|
+
"__API_ASSERT__ %s",
|
|
59
|
+
json.dumps({"name": name, "passed": passed, **redact(detail)}, separators=(",", ":")),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _fail(message: str | None, default: str, field: str | None) -> None:
|
|
64
|
+
if field is not None:
|
|
65
|
+
default = f"[{field}] {default}"
|
|
66
|
+
text = f"{message}\n {redact(default)}" if message else redact(default)
|
|
67
|
+
if soft_active():
|
|
68
|
+
record(text) # inside soft_assertions(): collect instead of raising
|
|
69
|
+
return
|
|
70
|
+
raise AssertionError(text)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _masked(sensitive: bool, field: str | None) -> bool:
|
|
74
|
+
"""Mask when asked to, or when the field name itself is sensitive (``token`` etc.).
|
|
75
|
+
|
|
76
|
+
The field may be a source expression such as ``body["access_token"]``, so each
|
|
77
|
+
identifier segment is checked as well as the whole name.
|
|
78
|
+
"""
|
|
79
|
+
if sensitive or field is None:
|
|
80
|
+
return sensitive
|
|
81
|
+
return is_sensitive_key(field) or any(
|
|
82
|
+
is_sensitive_key(seg) for seg in re.split(r"\W+", field) if seg
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
_MAX_FIELD_LEN = 80
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@functools.lru_cache(maxsize=64)
|
|
90
|
+
def _parse_source(filename: str) -> tuple[str, ast.AST] | None:
|
|
91
|
+
source = "".join(linecache.getlines(filename))
|
|
92
|
+
if not source:
|
|
93
|
+
return None
|
|
94
|
+
try:
|
|
95
|
+
return source, ast.parse(source, filename)
|
|
96
|
+
except (SyntaxError, ValueError):
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _call_name(func: ast.expr) -> str | None:
|
|
101
|
+
if isinstance(func, ast.Name):
|
|
102
|
+
return func.id
|
|
103
|
+
if isinstance(func, ast.Attribute):
|
|
104
|
+
return func.attr
|
|
105
|
+
return None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _is_field_like(node: ast.expr) -> bool:
|
|
109
|
+
"""True for name-like expressions: ``x``, ``a.b``, ``body["k"][0]``, ``resp.json()["id"]``.
|
|
110
|
+
|
|
111
|
+
Literals and anything built from them (dicts, f-strings, concatenation, calls
|
|
112
|
+
with arguments) are rejected: their source text says nothing about which
|
|
113
|
+
field is checked and may contain secret values written inline in the test.
|
|
114
|
+
"""
|
|
115
|
+
if isinstance(node, ast.Name):
|
|
116
|
+
return True
|
|
117
|
+
if isinstance(node, ast.Attribute):
|
|
118
|
+
return _is_field_like(node.value)
|
|
119
|
+
if isinstance(node, ast.Subscript):
|
|
120
|
+
index = node.slice
|
|
121
|
+
key_ok = isinstance(index, ast.Name) or (
|
|
122
|
+
isinstance(index, ast.Constant) and isinstance(index.value, (str, int))
|
|
123
|
+
)
|
|
124
|
+
return key_ok and _is_field_like(node.value)
|
|
125
|
+
if isinstance(node, ast.Call):
|
|
126
|
+
return not node.args and not node.keywords and _is_field_like(node.func)
|
|
127
|
+
return False
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _infer_field(helper: str, filename: str, lineno: int) -> str | None:
|
|
131
|
+
"""Return the source text of the first argument of the *helper* call at *lineno*."""
|
|
132
|
+
parsed = _parse_source(filename)
|
|
133
|
+
if parsed is None:
|
|
134
|
+
return None
|
|
135
|
+
source, tree = parsed
|
|
136
|
+
best: ast.Call | None = None
|
|
137
|
+
for node in ast.walk(tree):
|
|
138
|
+
if (
|
|
139
|
+
isinstance(node, ast.Call)
|
|
140
|
+
and node.args
|
|
141
|
+
and _call_name(node.func) == helper
|
|
142
|
+
and node.lineno <= lineno <= (node.end_lineno or node.lineno)
|
|
143
|
+
):
|
|
144
|
+
# Innermost call wins if several span the line.
|
|
145
|
+
if best is None or node.lineno >= best.lineno:
|
|
146
|
+
best = node
|
|
147
|
+
if best is None or not _is_field_like(best.args[0]):
|
|
148
|
+
return None
|
|
149
|
+
segment = ast.get_source_segment(source, best.args[0])
|
|
150
|
+
if not segment:
|
|
151
|
+
return None
|
|
152
|
+
segment = " ".join(segment.split())
|
|
153
|
+
if len(segment) > _MAX_FIELD_LEN:
|
|
154
|
+
segment = segment[: _MAX_FIELD_LEN - 1] + "…"
|
|
155
|
+
return segment
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _resolve_field(field: str | None) -> str | None:
|
|
159
|
+
"""Explicit *field*, else the source text of the caller's first argument.
|
|
160
|
+
|
|
161
|
+
Must be called directly from a public helper: it reads the helper's name
|
|
162
|
+
and its caller's location from the stack.
|
|
163
|
+
"""
|
|
164
|
+
if field is not None:
|
|
165
|
+
return field
|
|
166
|
+
frame = inspect.currentframe()
|
|
167
|
+
try:
|
|
168
|
+
helper_frame = frame.f_back if frame else None
|
|
169
|
+
caller = helper_frame.f_back if helper_frame else None
|
|
170
|
+
if helper_frame is None or caller is None:
|
|
171
|
+
return None
|
|
172
|
+
return _infer_field(helper_frame.f_code.co_name, caller.f_code.co_filename, caller.f_lineno)
|
|
173
|
+
except Exception: # inference is best-effort; never break an assertion over it
|
|
174
|
+
return None
|
|
175
|
+
finally:
|
|
176
|
+
del frame
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _show(value: Any, sensitive: bool) -> Any:
|
|
180
|
+
"""Value as it may appear in sentinels and failure messages."""
|
|
181
|
+
return mask(value) if sensitive else value
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def assert_equal(
|
|
185
|
+
actual: str,
|
|
186
|
+
expected: str,
|
|
187
|
+
*,
|
|
188
|
+
message: str | None = None,
|
|
189
|
+
sensitive: bool = False,
|
|
190
|
+
field: str | None = None,
|
|
191
|
+
) -> None:
|
|
192
|
+
"""Assert *actual* equals *expected*."""
|
|
193
|
+
field = _resolve_field(field)
|
|
194
|
+
passed = actual == expected
|
|
195
|
+
hide = _masked(sensitive, field)
|
|
196
|
+
shown_actual, shown_expected = _show(actual, hide), _show(expected, hide)
|
|
197
|
+
_emit("assert_equal", passed, field, expected=shown_expected, actual=shown_actual)
|
|
198
|
+
if not passed:
|
|
199
|
+
_fail(message, f"Expected {shown_expected!r}, got {shown_actual!r}.", field)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def assert_not_equal(
|
|
203
|
+
actual: str,
|
|
204
|
+
expected: str,
|
|
205
|
+
*,
|
|
206
|
+
message: str | None = None,
|
|
207
|
+
sensitive: bool = False,
|
|
208
|
+
field: str | None = None,
|
|
209
|
+
) -> None:
|
|
210
|
+
"""Assert *actual* does not equal *expected*."""
|
|
211
|
+
field = _resolve_field(field)
|
|
212
|
+
passed = actual != expected
|
|
213
|
+
hide = _masked(sensitive, field)
|
|
214
|
+
shown_actual, shown_expected = _show(actual, hide), _show(expected, hide)
|
|
215
|
+
_emit("assert_not_equal", passed, field, expected=shown_expected, actual=shown_actual)
|
|
216
|
+
if not passed:
|
|
217
|
+
_fail(
|
|
218
|
+
message,
|
|
219
|
+
f"Expected value to differ from {shown_expected!r}, got {shown_actual!r}.",
|
|
220
|
+
field,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def assert_equal_ignore_case(
|
|
225
|
+
actual: str,
|
|
226
|
+
expected: str,
|
|
227
|
+
*,
|
|
228
|
+
message: str | None = None,
|
|
229
|
+
sensitive: bool = False,
|
|
230
|
+
field: str | None = None,
|
|
231
|
+
) -> None:
|
|
232
|
+
"""Assert *actual* equals *expected*, ignoring case."""
|
|
233
|
+
field = _resolve_field(field)
|
|
234
|
+
passed = actual.lower() == expected.lower()
|
|
235
|
+
hide = _masked(sensitive, field)
|
|
236
|
+
shown_actual, shown_expected = _show(actual, hide), _show(expected, hide)
|
|
237
|
+
_emit("assert_equal_ignore_case", passed, field, expected=shown_expected, actual=shown_actual)
|
|
238
|
+
if not passed:
|
|
239
|
+
_fail(
|
|
240
|
+
message,
|
|
241
|
+
f"Expected {shown_expected!r} (case-insensitive), got {shown_actual!r}.",
|
|
242
|
+
field,
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def assert_contains(
|
|
247
|
+
actual: str,
|
|
248
|
+
expected_substring: str,
|
|
249
|
+
*,
|
|
250
|
+
message: str | None = None,
|
|
251
|
+
sensitive: bool = False,
|
|
252
|
+
field: str | None = None,
|
|
253
|
+
) -> None:
|
|
254
|
+
"""Assert *actual* contains *expected_substring*."""
|
|
255
|
+
field = _resolve_field(field)
|
|
256
|
+
passed = expected_substring in actual
|
|
257
|
+
hide = _masked(sensitive, field)
|
|
258
|
+
shown_actual = _show(actual, hide)
|
|
259
|
+
shown_substring = _show(expected_substring, hide)
|
|
260
|
+
_emit("assert_contains", passed, field, expected_substring=shown_substring, actual=shown_actual)
|
|
261
|
+
if not passed:
|
|
262
|
+
_fail(message, f"Expected {shown_actual!r} to contain {shown_substring!r}.", field)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def assert_matches(
|
|
266
|
+
actual: str,
|
|
267
|
+
pattern: str,
|
|
268
|
+
*,
|
|
269
|
+
message: str | None = None,
|
|
270
|
+
sensitive: bool = False,
|
|
271
|
+
field: str | None = None,
|
|
272
|
+
) -> None:
|
|
273
|
+
"""Assert *actual* matches the regex *pattern*."""
|
|
274
|
+
field = _resolve_field(field)
|
|
275
|
+
passed = re.search(pattern, actual) is not None
|
|
276
|
+
shown_actual = _show(actual, _masked(sensitive, field))
|
|
277
|
+
_emit("assert_matches", passed, field, pattern=pattern, actual=shown_actual)
|
|
278
|
+
if not passed:
|
|
279
|
+
_fail(message, f"Expected {shown_actual!r} to match /{pattern}/.", field)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def assert_is_empty(
|
|
283
|
+
actual: str,
|
|
284
|
+
*,
|
|
285
|
+
message: str | None = None,
|
|
286
|
+
sensitive: bool = False,
|
|
287
|
+
field: str | None = None,
|
|
288
|
+
) -> None:
|
|
289
|
+
"""Assert *actual* is an empty string."""
|
|
290
|
+
field = _resolve_field(field)
|
|
291
|
+
passed = actual == ""
|
|
292
|
+
shown_actual = _show(actual, _masked(sensitive, field))
|
|
293
|
+
_emit("assert_is_empty", passed, field, actual=shown_actual)
|
|
294
|
+
if not passed:
|
|
295
|
+
_fail(message, f"Expected empty string, got {shown_actual!r}.", field)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def assert_is_not_empty(
|
|
299
|
+
actual: str,
|
|
300
|
+
*,
|
|
301
|
+
message: str | None = None,
|
|
302
|
+
sensitive: bool = False,
|
|
303
|
+
field: str | None = None,
|
|
304
|
+
) -> None:
|
|
305
|
+
"""Assert *actual* is a non-empty string."""
|
|
306
|
+
field = _resolve_field(field)
|
|
307
|
+
passed = actual != ""
|
|
308
|
+
_emit("assert_is_not_empty", passed, field, actual=_show(actual, _masked(sensitive, field)))
|
|
309
|
+
if not passed:
|
|
310
|
+
_fail(message, "Expected non-empty string, got empty string.", field)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def assert_matches_schema(
|
|
314
|
+
actual: object,
|
|
315
|
+
schema: dict[str, Any],
|
|
316
|
+
*,
|
|
317
|
+
message: str | None = None,
|
|
318
|
+
field: str | None = None,
|
|
319
|
+
) -> None:
|
|
320
|
+
"""Assert *actual* conforms to the given JSON Schema, collecting all errors.
|
|
321
|
+
|
|
322
|
+
Values under sensitive keys (``token``, ``password``, …) are masked in the
|
|
323
|
+
sentinel's ``actual`` payload.
|
|
324
|
+
"""
|
|
325
|
+
field = _resolve_field(field)
|
|
326
|
+
import jsonschema
|
|
327
|
+
|
|
328
|
+
validator = jsonschema.Draft7Validator(schema)
|
|
329
|
+
errors = sorted(validator.iter_errors(actual), key=lambda e: list(e.path))
|
|
330
|
+
passed = not errors
|
|
331
|
+
error_messages = [schema_error_message(e) for e in errors]
|
|
332
|
+
_emit(
|
|
333
|
+
"assert_matches_schema",
|
|
334
|
+
passed,
|
|
335
|
+
field,
|
|
336
|
+
errors=error_messages,
|
|
337
|
+
schema=json.dumps(schema, indent=2, default=str),
|
|
338
|
+
actual=json.dumps(redact(actual), indent=2, default=str),
|
|
339
|
+
)
|
|
340
|
+
if not passed:
|
|
341
|
+
joined = "; ".join(error_messages)
|
|
342
|
+
_fail(message, f"Schema validation failed with {len(errors)} error(s): {joined}", field)
|