mardi 9 août 2016

API testing in C# - how do I compare entire JSON response in 1 assertion?

I am writing API tests with VS & C#

I have a test that looks like this:

[TestCase("buddy")]
public void CreatePet(string name)
{
    //Setup Request
    RestRequest = new RestRequest("pet", Method.POST);

    //Load JSON File
    string RequestBody = Utils.LoadJson("CreatePet.json");
    RestRequest.AddParameter("application/json", RequestBody, ParameterType.RequestBody);

    //Call REST Resource
    ResponseJSON = JSON_NET_GetResponse(RestRequest);

    //Verify Response JSON
    ((string) ResponseJSON.name).Should().Be("doggie");
    ((string) ResponseJSON.status).Should().Be("available");
    ((Int64) ResponseJSON.id).Should().BeGreaterThan(Int32.MaxValue);
    ((string) ResponseJSON.tags[0].name).Should().Be("string");
    ((int) ResponseJSON.tags.Count).Should().Be(1);    

Notice I have 5 asserts there. I am asserting on most of the lines of the returned JSon.

Is it possible to simply write 1 assert that ensures the entire Json is the same as it was last time? I assume I'd need to ignore certain things like GUIDs and timestamps... but otherwise I don't want to be line by line writing asserts for every single thing... in some cases my Json responses could be 50 lines long...

I'm assuming this is a solved problem and I don't want to reinvent the wheel. How do you guys do it?

The Json response for my test looks like this:

{
  "id": 379856466726,
  "category": {
    "id": 0,
    "name": "string"
  },
  "name": "doggie",
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "available"
}

Aucun commentaire:

Enregistrer un commentaire