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 |