assert-json-diff
This crate includes macros for comparing two JSON values. It is designed to give much
more helpful error messages than the standard assert_eq!
. It basically does a diff of the
two objects and tells you the exact differences. This is useful when asserting that two large
JSON objects are the same.
It uses the serde_json::Value
type to represent JSON.
Install
[]
= "1.0.0"
Partial matching
If you want to assert that one JSON value is "included" in another use
assert_json_include
:
extern crate assert_json_diff;
extern crate serde_json;
This will panic with the error message:
json atoms at path ".data.users[0].country.name" are not equal:
expected:
"Sweden"
actual:
"Denmark"
json atoms at path ".data.users[1].id" are not equal:
expected:
2
actual:
24
assert_json_include
allows extra data in actual
but not in expected
. That is so you can verify just a part
of the JSON without having to specify the whole thing. For example this test passes:
extern crate assert_json_diff;
extern crate serde_json;
However expected
cannot contain additional data so this test fails:
extern crate assert_json_diff;
extern crate serde_json;
That will print
json atom at path ".a.b" is missing from actual
Exact matching
If you want to ensure two JSON values are exactly the same, use assert_json_eq
.
extern crate assert_json_diff;
extern crate serde_json;
This will panic with the error message:
json atom at path ".a.b" is missing from lhs
License: MIT