4 minute read

Adding tests to a contract test collection

Now that you understand these options, you can choose the ones you want and then click on Create Contract Test to tell Postman to create a collection with requests in place for you. Once you've done that and looked at the Collection tab in the navigation pane, you will see that there is a collection available for you. This is a great start, but we are not trying to create a contract collection; we are trying to create a contract test, so you still have some more work to do.

You now have a collection that you can use for contract testing, but you aren't quite done yet. Postman has automatically created the structure and details of the request, but you need to define the actual tests that you want this collection to run. This is the point at which understanding the theory of contract testing becomes helpful. What kind of tests should you add to this collection? With contract tests, we want to make sure that the API is fulfilling the needs that we have as a consumer. We don't want to create a comprehensive set of tests that checks everything this API can do. We just want to add tests that verify that it provides the data we are interested in. In this case, we are pretending that we have a to-do list application, so we would only want to add tests that verify data for the kinds of requests that our application is running. We would then create tests that check that those specific calls give us back the data that we need.

Advertisement

I covered how to create tests in detail in Chapter 6, Creating Test Validation Scripts, so I won't go through it again in this chapter. However, one thing that can be helpful when creating contract tests is the Postman Interceptor.

Using the Postman Interceptor Postman can dynamically capture requests for you with its Interceptor. When creating contract tests, you want to create tests that demonstrate the actual needs of your application. The Interceptor allows you to do this. When it is running, it will capture all the requests that your application makes, allowing you to see the actual requests that you need for your contract test. Since we don't have a to-do list application to test against, I will use the swapi.dev API as an example. You can set up the Interceptor in Postman by following these steps:

1. First, you will need to have Chrome installed on your computer. I would also recommend that you close all tabs except one so that you don't get too much unwanted data. You can then install the Postman Interceptor by downloading it from the Chrome web store. You can download it directly from this link:

https://go.pstmn.io/interceptor-download.

2. Click on Add to Chrome and choose Add extension on the verification prompt to install the extension. 3. Once the extension has been installed, return to the Postman app and click on the Capture request and cookies with Postman icon, as shown in the following screenshot:

Figure 13.1 – Capture requests and cookies with Postman 4. On the Requests tab on the resulting flyout, set Source to Interceptor and enable the Capture Requests option. 5. If you want, you can also capture cookies by going to the Cookies tab and enabling the Capture Cookies option. 6. Select the Install Interceptor Bridge option to set up the bridge that will facilitate communication between the Postman app and the Interceptor extension that you just installed. 7. Once it has completed the setup, you will see that the INTERCEPTOR

CONNECTED status is green, as shown in the following screenshot:

Figure 13.2 – Interceptor connected

8. Ensure that the Save Requests to option is set to History. You can save the requests directly to a collection, but there will usually be a lot of requests that you don't need in there as well, so I would recommend saving it to your history.

With that, you have the Interceptor set up and running. You can now go to a website such as swapi.dev and perform some actions there. As you do so, Postman will save all the API requests in the History tab. You can go to the tab in the navigation pane and find the requests that you are interested in and add them to your collection. To do so, you can click on the Add request to Collection icon, as shown in the following screenshot:

Figure 13.3 – Adding an intercepted request to a collection This allows you to easily add the requests that your application sends to your contract testing collection. You will still need to create tests to check that the data and data structures are correct, but this can help you figure out what requests to send for contract tests. As I've already said, I'm not going to walk you through a step-by-step process for creating tests for these requests, but now that you know what requests to include and what kinds of parameters they need to have, you should be able to go through this and fill in the test's details. However, in order to understand this a bit better, let's take a look at what you would do in broad terms.

You would want to use the Interceptor in situations where you do not have an already defined schema for the API that you can create contract tests against. In that situation, you can use the Interceptor to help you "reverse-engineer" how the API works and what things it provides. So, in this example, we could look at the results given back by a couple of different people and, from there, figure out which fields are required (for example, the "name" and "height" fields).

This article is from: