Need more help on Snapshot testing in Postman

My question: I can’t get snapshot testing to work for me. I’ve taken the snapshot response, I’ve created a mock server with that response. What should my test script look like? Is there a pre-requisite step?

I’ve already tried: I’ve gone to the topic Snapshot testing for APIs using Postman but could not understand what their test script included.

Hi @rnfasoliz

Have you added the snapshot response as an “example” of your mock server like this?

Once you have this example you can add your tests in the “tests” tab to check the response data, for example;

Hope this helps…

Thanks for the reply, but my goal is to validate the json schema and not only the status code. It seemed like snapshot testing is the key. I know how to make a snapshot and create a mock server with that snapshot, but not sure on how the test script is written in order to validate the json schema.

Here is a schema validation example for a REST API, it should give you the base structure for whats needed in your test tab;

const schema = {
    "type": "object",
    "properties": {
        "id": {
      "type": "integer"
    },
    "category": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "name"
      ]
    },
    "name": {
      "type": "string"
    },
    "photoUrls": {
      "type": "array",
      "items": [
        {
          "type": "string"
        }
      ]
    },
    "tags": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "id": {
              "type": "integer"
            },
            "name": {
              "type": "string"
            }
          },
          "required": [
            "id",
            "name"
          ]
        }
      ]
    },
    "status": {
      "type": "string"
    }
  },
  "required": [
    "id",
    "category",
    "name",
    "photoUrls",
    "tags",
    "status"
  ]
};

pm.test("Validating the API schema", () => {
    pm.response.to.have.jsonSchema(schema);
});

Awesome thank you for the help!

1 Like