JSON assertions¶
JSONPath navigation and JSON Schema validation (requires the json extra).
JSON path navigation and schema validation mixin.
at_json_path ¶
Navigate to a JSON path and return a new builder with the matched value.
Uses JSONPath syntax (e.g. $.users[0].name). If multiple matches are found,
the value is a list of all matches. If exactly one match is found, the value is
unwrapped from the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
JSONPath expression. |
required |
Examples:
Usage:
data = {"users": [{"name": "Alice"}, {"name": "Bob"}]}
assert_that(data).at_json_path("$.users[0].name").is_equal_to("Alice")
assert_that(data).at_json_path("$.users[*].name").is_equal_to(["Alice", "Bob"])
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new instance with the extracted value |
Raises:
| Type | Description |
|---|---|
ValueError
|
if no match is found at the given path |
Source code in assertpy2/json_mixin.py
has_json_path ¶
Assert that the given JSON path exists in val.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
JSONPath expression. |
required |
Examples:
Usage:
data = {"meta": {"total": 5}}
assert_that(data).has_json_path("$.meta.total")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the path does not exist |
Source code in assertpy2/json_mixin.py
does_not_have_json_path ¶
Assert that the given JSON path does not exist in val.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
JSONPath expression. |
required |
Examples:
Usage:
data = {"status": "ok"}
assert_that(data).does_not_have_json_path("$.error")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the path exists |
Source code in assertpy2/json_mixin.py
matches_json_schema ¶
Assert that val conforms to the given JSON Schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
a JSON Schema as a dict. |
required |
Examples:
Usage:
schema = {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}
assert_that({"name": "Alice"}).matches_json_schema(schema)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not conform to the schema |
Source code in assertpy2/json_mixin.py
matches_json_schema_from_file ¶
Assert that val conforms to a JSON Schema loaded from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
path to a JSON file containing the schema. |
required |
Examples:
Usage:
assert_that(data).matches_json_schema_from_file("schemas/order.json")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not conform to the schema |
Source code in assertpy2/json_mixin.py
conforms_to_openapi ¶
conforms_to_openapi(
spec: dict[str, Any],
path: str,
method: str,
*,
status: str | int | None = None,
content_type: str = "application/json",
) -> Self
Assert that val conforms to an OpenAPI operation's response-body schema.
val is validated against the schema declared for the application/json response of the
method/path operation in spec. This checks only the response body of that one
operation - not request bodies, parameters, headers, or the spec as a whole.
OpenAPI 3.0 (its nullable keyword is honoured), 3.1, and Swagger 2.0 (schema declared directly
on the response, its x-nullable extension honoured) are all supported. $ref,
oneOf/allOf/anyOf, enum, and format all validate with full JSON-Schema
semantics, and every violation is reported with its JSON path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict[str, Any]
|
a parsed OpenAPI document (dict); loading YAML/JSON is the caller's job. |
required |
path
|
str
|
the operation's path template, e.g. |
required |
method
|
str
|
the HTTP method, e.g. |
required |
status
|
str | int | None
|
response status to validate against; defaults to |
None
|
content_type
|
str
|
response content type; defaults to |
'application/json'
|
Examples:
Usage:
spec = {...} # your parsed OpenAPI document
assert_that(response.json()).conforms_to_openapi(spec, "/orders/{id}", "get")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not conform to the response schema |
ValueError
|
if the operation, status, or content type is not found in the spec |
Source code in assertpy2/json_mixin.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |