DEV Community

Cover image for Test JSON for .NET
LateApexEarlySpeed
LateApexEarlySpeed

Posted on

Test JSON for .NET

Almost all the developers have requirement to write unit test. Sometimes you need to test whether specific JSON text is as expected, what will you decide to implement for it ?

If you want to test whole JSON is same (means JSON-level equivalent) and meantime you have the data model which is just for coming JSON, you can test by normal way everyone knows: deserialize JSON to data model instance, then compare with expected another data model instance.

But there should be other scenarios …

  1. you want to compare two JSONs are JSON-equivalent without creating additional data model
  2. Further, you want to assert some JSON nodes inside whole JSON body should meet some conditions

Scenario 1:

As you know, even if 2 literal strings representing JSON are not text-level same, it is still possible that they are JSON-equivalent, just one simple example:

{
  "a": 1,
  "b": 2
}
Enter fullscreen mode Exit fullscreen mode
{
  "b": 2,
  "a": 1
}
Enter fullscreen mode Exit fullscreen mode

They are JSON-equivalent.

Then how to deal with this sort of “equivalent” (Imagine possible recursive cases in future)?

Solution

There is nuget package ‘LateApexEarlySpeed.Xunit.Assertion.Json’, by using that, comparison can be achieved as:

Equivalent for simple JSON

this library also considered recursive and format cases:

Equivalent for complex JSON

When assertion failed, how to better know where the JSON node failed to assert and what is the cause ? Let’s try this assertion statement:

Different JSON comparison

and then you will get following exception to tell that there are different number (2 and 0) in JSON location: /a/1 :

Assert exception

The JSON location ‘/a/1’ is with standard JSON pointer format.

Scenario 2:

Now let’s say you don’t need to compare whole JSON body’s equivalent but have more concrete test requirement, for example:

the JSON needs to have property ‘a’ with any value; have property ‘b’ with array with max length of 2 and each item should have property ‘prop’ with numeric value no large than 10; have property ‘c’ with a value be (JSON-) equivalent to {“c1”:1, “c2”:2}.

No limit for other factors.

normally you may need to write test for this as:

Traditional way

Not only it costs more code, but also imagine if assertion failed inside ‘foreach’ loop, it is hard to get which array item error is from by exception.

Solution

With help of library ‘LateApexEarlySpeed.Xunit.Assertion.Json’, test code can be:

New way

The code should be straightforward now and it is fluent manner.

Notice the ‘Equivalent’ method mentioned in scenario 1 can also be used for any JSON node location.

Sometimes you want to have custom validation logic unit in specific JSON node. Let’s say you want to

test value of property ‘c’ should have same numeric values for sub properties ‘c1’ and ‘c2’

then you can write as:

Support custom validation logic

the ‘HasCustomValidation’ method accepts custom validation provider and custom error message provider and has several overloads to select.

If property ‘c2’ is 2 in ‘actualJson’, then assertion will fail from your custom validation code and will get error message you provide:

Assert exception

The available validation methods are in wiki, document is in repo.

Conclusion

This article introduced one powerful and flexible JSON test way which should be able to cover most test requirement for JSON. Hope it helps, any feedback is welcomed and thanks for giving github star.

BTW, this JSON assertion extensions is based on core library which targets standard JSONSchema spec and the core library is also in same repo.

Top comments (0)