A Go test assertion library for verifying that two representations of JSON are semantically equal
Branch: master
Clone or download
Latest commit 45f075b Feb 8, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml Run coverage in Travis Feb 7, 2019
LICENSE Initial commit Oct 26, 2018
README.md
array.go be friendlier with the assert message when the JSON representation ar… Feb 1, 2019
boolean.go rename level to path Feb 1, 2019
core.go Improve error message when type cannot be identified Feb 6, 2019
examples_test.go
exports.go Add package docs Feb 6, 2019
go.mod remove unnecessary dependency Nov 4, 2018
go.sum Remove unused references Jan 26, 2019
integration_test.go Improve error message when type cannot be identified Feb 6, 2019
number.go rename level to path Feb 1, 2019
object.go rename level to path Feb 1, 2019
string.go

README.md

jsonassert

Mentioned in Awesome Go Build Status Go Report Card Coverage Status Latest version Go Documentation License

jsonassert is a Go test assertion library for verifying that two representations of JSON are semantically equal.

Usage

Create a new *jsonassert.Asserter in your test and use this to make assertions against your JSON payloads:

func TestWhatever(t *testing.T) {
    ja := jsonassert.New(t)
    // find some sort of payload
    ja.Assertf(payload, `
    {
        "name": "%s",
        "age": %d,
        "skills": [
            { "name": "martial arts", "level": 99 },
            { "name": "intelligence", "level": 100 },
            { "name": "mental fortitude", "level": 4 }
        ]
    }`, "River Tam", 16)
}

You may pass in fmt.Sprintf arguments after the expected JSON structure.

ja.Assertf() currently supports assertions against strings only.

Check for presence only

Some properties of a JSON payload may be difficult to know in advance. E.g. timestamps, UUIDs, or other randomly assigned values.

For these types of values, place the string "<<PRESENCE>>" as the expected value, and jsonassert will only verify that this key exists (i.e. the actual JSON has the expected key, and its value is not null), but this does not check its value.

For example:

func TestWhatever(t *testing.T) {
    ja := jsonassert.New(t)
    ja.Assertf(`
    {
        "time": "2019-01-28T21:19:42",
        "uuid": "94ae1a31-63b2-4a55-a478-47764b60c56b"
    }`, `
    {
        "time": "<<PRESENCE>>",
        "uuid": "<<PRESENCE>>"

    }`)
}

The above will pass your test, but:

func TestWhatever(t *testing.T) {
    ja := jsonassert.New(t)
    ja.Assertf(`
    {
        "date": "2019-01-28T21:19:42",
        "uuid": null
    }`, `
    {
        "time": "<<PRESENCE>>",
        "uuid": "<<PRESENCE>>"
    }`)
}

The above will fail your tests because the time key was not present in the actual JSON, and the uuid was null.

Docs

You can find the GoDocs for this package here.

Contributing

Contributions are welcome. Please discuss feature requests in an issue before opening a PR.