By using Swagger, we can easily import API information to Postman API Collection.
Copy the URL: https://localhost:7123/swagger/v1/swagger.json
Open the Postman application. If you don't have an account, please create one and download the application.
Click on the Import Button. It will open a popup like the one below.
Paste the copied URL in the highlighted textbox.
Click the Import Button once again in the box, It will create the collection as follows.
API Collection Highlights
We will set Authorization globally here in this tab.
In the Variables tab.
We can override the values below the image for our deployment environment settings.
- If Development baseUrl: https://localhost:7123
- If Testing baseUrl: https://test-api.domain.com
More details as below as an image.
While hitting the Send button (in Blue color), our endpoint will be triggered and get the API response.
The proper username and password will provide the results below.
Practical API Testing Techniques for Accelerating Development
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Body isSuccess as true", function () {
pm.expect(pm.response.text()).to.include("\"IsSuccess\":true");
});
pm.test("Body message", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.Message).to.include("Login Success");
});
pm.test("Body Email Found", function () {
var jsonData = pm.response.json();
console.log("Value of Email in the Response Body - " + jsonData.Data.Email);
pm.expect(jsonData.Data.Email).to.not.eql(null);
pm.expect(jsonData.Data.Email).to.not.eql("");
pm.expect(jsonData.Data.Email).to.be.an('string');
});
The below sample code for testing scripts can learn about from here https://learning.postman.com/docs/tests-and-scripts/write-scripts/test-examples/.
Basic scripts help developers unit-test their endpoints following the TDD approach.
In the example below, we see that the response was 200, but the email data in the response was missing, causing one test to fail.
The response below was passed with a score of 4/4.
In this article, most concepts are presented visually, making them easier to understand compared to lengthy text. Please share any suggestions for improvement.