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.
Now that you have your working endpoint it’s time to test it. Click on the Tests tab like in the screen below.
First we need to test for the response Status Code. We expect it to be OK – 200. For that we write a test:
1 2 3 |
pm.test("response should be ok", function () { pm.response.to.be.ok; }); |
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:
Now let’s add another test, which will assert if the response body is in JSON format.
1 2 3 |
pm.test("response should have json body", function () { pm.response.to.be.json; }); |
Let’s continue with the test with assert if the response time is less than 200ms.
1 2 3 |
pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); }); |
If we make the request again, all 3 tests are run.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
var schema = { "definitions": {}, "$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://example.com/root.json", "type": "array", "title": "The Root Schema", "items": { "$id": "#/items", "type": "object", "title": "The Items Schema", "required": [ "date", "temperatureC", "temperatureF", "summary" ], "properties": { "date": { "$id": "#/items/properties/date", "type": "string", "title": "The Date Schema", "default": "", "examples": [ "2020-01-27T21:03:18.0246295+01:00" ], "pattern": "^(.*)$" }, "temperatureC": { "$id": "#/items/properties/temperatureC", "type": "integer", "title": "The Temperaturec Schema", "default": 0, "examples": [ -17 ] }, "temperatureF": { "$id": "#/items/properties/temperatureF", "type": "integer", "title": "The Temperaturef Schema", "default": 0, "examples": [ 2 ] }, "summary": { "$id": "#/items/properties/summary", "type": "string", "title": "The Summary Schema", "default": "", "examples": [ "Freezing" ], "pattern": "^(.*)$" } } } }; pm.test('Schema is valid', function() { pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true; }); |
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.
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!
1 2 3 4 5 6 7 8 |
... "required": [ "date", "temperature", "temperatureF", "summary" ], ... |
When we run the tests again the last test fails. That’ because the schema does not match with the response JSON body.
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).
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.
Leave a Reply
You must belogged in to post a comment.