Writing tests, most of the times is not a breeze. Writing good tests with acceptable code coverage takes time, effort, practice, and patience. Probably that's why you would find "Write Tests" as the last to-do item in most developers' task list. This situation gets worse in the case of startups or early-stage companies because the time, effort and resources spent on writing tests are not justified by the business.
In this lesson, we will talk about Snapshot Testing, a style of testing which removes much of the manual coding and boilerplate, leaving the test runner to compare the produced result against a saved snapshot. Snapshot testing became a major thing for UI tests, thanks to Jest test runner by Facebook that includes it by default. Before starting on how to get this working in Postman, let us first understand what is Snapshot Testing in principle.
Imagine a unit test.
const add = (a, b) => a + b;
it("adds numbers", () => {
expect(add(2, 3)).toBe(5);
});
The test computes a value using a call to add(2, 3)
and compares it to a hard-coded value of 5
. How did we get this expected value 5
? Well, I (the human developer) took a piece of paper and a pencil and added 2 + 3
. Easy.
Imagine a real-world unit test. Would computing the expected value be as simple as adding two small numbers? No. A more complex algorithm would generate a large object as a result, not a single number. For example, if we are unit testing an API response that response could be a really big nested JSON object.
Ideally, you would either write tests for each and every individual property of the response or write a huge schema that this response will validate against. This sounds good if you have 2–5 requests but imagine doing this same tedious task for 100s of requests.
Now imagine what if you could test this something like
it("returns top seller item", () =>
api.get().then((item) => expect(item).toMatchSnapshot()));
The item
returned from the API should match its saved snapshot. Here a snapshot is nothing but just the Gold Standard of how your item
should look like.
Ideally, a snapshot should be exactly equal to the result that you are trying to test but in reality, this might not be true (especially in the case of APIs). Therefore, based on your specific needs and type of responses that you are trying to test, you can choose any of the following snapshot testing strategies for your APIs:
Now that we have seen what is a Snapshot and what Snapshot Testing is about, let us build it out for the requests inside Postman. Postman is an API Development Environment (ADE) which means it comes with all the building blocks that we would need to achieve snapshot testing.
pm.sendRequest()
to fetch the snapshot.To help you get started with snapshot testing I have already published a Postman collection with all the above strategies. Fork the sample collection in the next section.
To set up Snapshot Testing for your APIs you can use this collection as reference.
snapshotURL
in Collection Variables with the Mock server url that you created above.This collection contains three different Snapshot testing strategies. You can choose any of them according to your use case and your response structure.
So as you can see how simply we created our very own snapshot testing framework using Postman. It also fits very well with other testing strategies like Consumer-driven Contract Testing.
For more interesting workflows and testing strategies, follow Postman Engineering.