image Testing .NET Core 3.1 API protected by JWT token image HTTP load testing with Bombardier part 1

HTTP testing with Postman

Developing, testing and upgrading your Web APIs can be a challenge. Luckily for us, there are many tools we can use to make our development more productive, our deployments safer and feeling better as custodians of our apps.

In this short post I would like to introduce you to the testing with Postman. Postman is a platform for API development which includes REST API client, API designer and documentation and API testing.

There is a good testing startup guide on the Postman page, but I would like to expand it a bit. To start with testing we need an API. I’ve created a simple .NET Core Web API from the default template with only one GET endpoint for weather forecast. Let’s run the application and switch to Postman.

In Postman create new request, set the name “Get Weather Data”, set GET method and enter the full endpoint URL. In my case the URL is https://localhost:5001/weatherforecast. Now click on the button Send. If everything is setup properly you should get back the JSON response below.

Figure 1: Setup Postman and run the request.

Now that you have your working endpoint it’s time to test it. Click on the Tests tab like in the screen below.

Figure 2: Click on Tests tab to open the test section.

First we need to test for the response Status Code. We expect it to be OK – 200. For that we write a test:

This is a Postman syntax, you can read more about it here.

When you click the Send button again, the test is run. We can see this below:

Figure 3: When we make the request, the test is run and passes.

Now let’s add another test, which will assert if the response body is in JSON format.

Let’s continue with the test with assert if the response time is less than 200ms.

If we make the request again, all 3 tests are run.

Figure 4: The result of passing tests.

This is good, but let’s add one more test. We will test whether the response JSON has the correct schema. I can not stress enough, how this is important. For example, if you change the JSON contract of the endpoint (without the API versioning) and you deploy it without this kind of tests, you are risking errors with your API consumers. So please, do test the contracts of your API endpoints, otherwise the error will be discovered too late, maybe even in production. And this is a no – no! So, here is the test:

This might look scary. Schema variable defined above can be auto-generated using a JSON schema generator. Just enter the JSON payload and it will generate your TV4 schema, which you can then test with the response.

Now, let’s run all the tests.

Figure 5: All tests are green!

All test are green! Let’s modify the schema a bit to get a failing test. Change the temperatureC to temperature in the “required” section, so the returned parameter is different!

When we run the tests again the last test fails. That’ because the schema does not match with the response JSON body.

Figure 6: Schema test fails.

When you have many endpoints in your Web API, you can use Runner within the Postman. It can be found in the toolbar. That’s one useful feature which allows you to run multiple tests with one click! Also I recommend you to setup environment variables, so you can run tests in multiple environments (Test, UAT and the like).

Figure 7: Open runner in the toolbar.
Figure 8: Select the API collection and run the tests.
Figure 9: Runner results.

And that’s it, really. Please read the Postman docs for more information about the syntax and to get some other ideas on what to test.

Let me also point out that Postman is only one option to do this kind of testing. In .NET we normally use NUnit or xUnit and store the tests with our source code.

Main image was taken from Postman website.

About Miha
I've always been fascinated by computers. I am happy that I can work in this creative field of software development, where I can solve problems. I am also an avid cyclist :) and I love to learn...a lot!
Related Posts
  • All
  • By Author
  • By Category
  • By Tag

Leave a Reply