#serde-json #json-diff #json #testing #error-message

dev assert-json-diff

Easily compare two JSON values and get great output

11 releases (stable)

2.0.2 Jun 29, 2022
2.0.1 Feb 14, 2021
2.0.0 Jan 23, 2021
1.1.0 Jul 12, 2020
0.2.1 Nov 17, 2018

#3 in Development tools

Download history 75695/week @ 2023-12-23 151231/week @ 2023-12-30 183960/week @ 2024-01-06 184178/week @ 2024-01-13 185624/week @ 2024-01-20 178694/week @ 2024-01-27 177369/week @ 2024-02-03 178499/week @ 2024-02-10 177846/week @ 2024-02-17 193024/week @ 2024-02-24 202156/week @ 2024-03-02 196193/week @ 2024-03-09 198806/week @ 2024-03-16 196491/week @ 2024-03-23 204841/week @ 2024-03-30 174006/week @ 2024-04-06

808,785 downloads per month
Used in 1,100 crates (110 directly)

MIT license

43KB
884 lines

Crates.io Docs dependency status Build status maintenance-status

assert-json-diff

This crate includes macros for comparing two serializable values by diffing their JSON representations. 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 and serde_json to perform the serialization.

Partial matching

If you want to assert that one JSON value is "included" in another use assert_json_include:

use assert_json_diff::assert_json_include;
use serde_json::json;

let a = json!({
    "data": {
        "users": [
            {
                "id": 1,
                "country": {
                    "name": "Denmark"
                }
            },
            {
                "id": 24,
                "country": {
                    "name": "Denmark"
                }
            }
        ]
    }
});

let b = json!({
    "data": {
        "users": [
            {
                "id": 1,
                "country": {
                    "name": "Sweden"
                }
            },
            {
                "id": 2,
                "country": {
                    "name": "Denmark"
                }
            }
        ]
    }
});

assert_json_include!(actual: a, expected: b)

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:

use assert_json_diff::assert_json_include;
use serde_json::json;

assert_json_include!(
    actual: json!({
        "a": { "b": 1 },
    }),
    expected: json!({
        "a": {},
    })
)

However expected cannot contain additional data so this test fails:

use assert_json_diff::assert_json_include;
use serde_json::json;

assert_json_include!(
    actual: json!({
        "a": {},
    }),
    expected: json!({
        "a": { "b": 1 },
    })
)

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.

use assert_json_diff::assert_json_eq;
use serde_json::json;

assert_json_eq!(
    json!({ "a": { "b": 1 } }),
    json!({ "a": {} })
)

This will panic with the error message:

json atom at path ".a.b" is missing from lhs

Further customization

You can use assert_json_matches to further customize the comparison.

License: MIT

Dependencies

~360–770KB
~17K SLoC