All Projects → kinbiko → jsonassert

kinbiko / jsonassert

Licence: MIT license
A Go test assertion library for verifying that two representations of JSON are semantically equal

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to jsonassert

beJS
Simple, light-weight assertions framework for javascript
Stars: ✭ 12 (-88.24%)
Mutual labels:  assertions, assertion-library
fluentcheck
Fluent assertions for Python
Stars: ✭ 79 (-22.55%)
Mutual labels:  assertions, assertion-library
json matcher
Library for simplifying data verification in functional tests for your JSON-based APIs
Stars: ✭ 24 (-76.47%)
Mutual labels:  assertions, assertion-library
Enzyme
JavaScript Testing utilities for React
Stars: ✭ 19,781 (+19293.14%)
Mutual labels:  assertions, assertion-library
Pester
Pester is the ubiquitous test and mock framework for PowerShell.
Stars: ✭ 2,620 (+2468.63%)
Mutual labels:  assertions
Expekt
BDD assertion library for Kotlin
Stars: ✭ 163 (+59.8%)
Mutual labels:  assertions
Should.js
BDD style assertions for node.js -- test framework agnostic
Stars: ✭ 1,908 (+1770.59%)
Mutual labels:  assertions
Zunit
A powerful testing framework for ZSH projects
Stars: ✭ 140 (+37.25%)
Mutual labels:  assertions
chai
BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
Stars: ✭ 7,842 (+7588.24%)
Mutual labels:  assertions
superdeno
Super-agent driven library for testing Deno HTTP servers.
Stars: ✭ 119 (+16.67%)
Mutual labels:  assertions
Gotest.tools
A collection of packages to augment the go testing package and support common patterns.
Stars: ✭ 205 (+100.98%)
Mutual labels:  assertions
Checkmate
Fast and versatile argument checks
Stars: ✭ 174 (+70.59%)
Mutual labels:  assertions
Check Types.js
MOVED TO GITLAB
Stars: ✭ 232 (+127.45%)
Mutual labels:  assertions
Assertj Core
AssertJ is a library providing easy to use rich typed assertions
Stars: ✭ 2,085 (+1944.12%)
Mutual labels:  assertions
K9
Rust testing library
Stars: ✭ 194 (+90.2%)
Mutual labels:  assertions
Property Validator
✅ Easy property validation for JavaScript, Node and Express.
Stars: ✭ 153 (+50%)
Mutual labels:  assertions
Truss
Assertions API for Clojure/Script
Stars: ✭ 239 (+134.31%)
Mutual labels:  assertions
Testify
A toolkit with common assertions and mocks that plays nicely with the standard library
Stars: ✭ 14,996 (+14601.96%)
Mutual labels:  assertions
Fluentassertions
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit tests. Targets .NET Framework 4.7, .NET Core 2.1 and 3.0, as well as .NET Standard 2.0 and 2.1. Supports the unit test frameworks MSTest2, NUnit3, XUnit2, MSpec, and NSpec3.
Stars: ✭ 2,449 (+2300.98%)
Mutual labels:  assertions
Codejam
Set of handy reusable .NET components that can simplify your daily work and save your time when you copy and paste your favorite helper methods and classes from one project to another
Stars: ✭ 217 (+112.75%)
Mutual labels:  assertions

logo

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

It's difficult to confirm that a JSON payload, e.g. a HTTP request or response body, does indeed look the way you want using the built-in Go testing package. jsonassert is an easy-to-use 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
	name := "River Tam"
	age := 16
	ja.Assertf(payload, `
	{
		"name": "%s",
		"age": %d,
		"averageTestScore": "%s",
		"skills": [
			{ "name": "martial arts", "level": 99 },
			{ "name": "intelligence", "level": 100 },
			{ "name": "mental fortitude", "level": 4 }
		]
	}`, name, age, "99%")
}

You may pass in fmt.Sprintf arguments after the expected JSON structure. This feature may be useful for the case when you already have variables in your test with the expected data or when your expected JSON contains a % character which could be misinterpreted as a format directive.

ja.Assertf() 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.

Ignore ordering in arrays

If your JSON payload contains an array with elements whose ordering is not deterministic, then you can use the "<<UNORDERED>>" directive as the first element of the array in question:

func TestUnorderedArray(t *testing.T) {
	ja := jsonassert.New(t)
	payload := `["bar", "foo", "baz"]`
	ja.Assertf(payload, `["foo", "bar", "baz"]`)                  // Order matters, will fail your test.
	ja.Assertf(payload, `["<<UNORDERED>>", "foo", "bar", "baz"]`) // Order agnostic, will pass your test.
}

Docs

You can find the GoDocs for this package here.

Contributing

Contributions are welcome. Please read the contribution guidelines and discuss feature requests in an issue before opening a PR.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].