Hello there 🤓, today I want to give you a gist on a tool I have been using both at work and in my personal projects for testing the APIs I write.
Postman, this to me is by far one of the best tools for running API tests and automation if needed, I have been on this tool for more than a year now.
In the beginning I used it as any other REST API client, just add in my requests and see a positive response come in but there was so much I could have achieved if only I knew this.
So today I would like you to join me on a short adventure of writing a simple API test with Postman, let's go!.
STEPS 🪜
💻 Install Postman :
Installing the Postman app is really easy, visit this page and obtain your platform version (Mac, Windows, Linux).
🍕 Add Environment Variable :
Environment variables in Postman aids you when setting up your request, a good example is setting your API url for re-use in other places. To do this, Open your installed Postman app and click on the Manage Environment
button at the top right corner of the window as seen below.
You will be greeted with a popup window. At the bottom right corner, click on the Add
button as seen below.
In the next view you will find a section to enter your environment name
, set your variable name
, add an initial value
and lastly a current value
, find an example below.
📝 To follow this test, you can set your initial value and current value to the URL below.
http://play.jefferyclonne.com/code/postman-training/index.php?limit=0
Initial value:
This is the value shared to your team or publicly when you decide to share your request with others.Current value:
This is the value used when Postman is making requests using your environment, it is an ephemeral value that does not get saved, only exist locally.
Now Add
and exit out of the window, check the Environments
drop-down to make sure yours is selected, as seen below.
🧱 Add Test Request :
If you are using the test URL above, follow this setup. In your Postman window click on the +
button as shown below.
Now in the address bar enter your environment variable {{url}}
and click on the Send
button to make sure the API is reachable, as seen below.
🎄 Add Test Cases :
Now that we have everything setup (wheew! 😅), lets start writing our test cases! 🤩.
In your Postman window select the Tests
tab to get started as seen below.
Now lets write our cases:
- 1.
Test status response is 200
: In this test we try to check if the response from the API has a status of200
, meaning everything isOK
.
♻️ Add the following code and click on the Send
button to run this test, now in the Response
section click on the Test Results
tab to check if your test case passed, as seen below.
pm.test("Test status response is 200", function () {
pm.response.to.have.status(200);
});
- 2.
Test response output status
: From our API response we have astatus
variable, now lets check to make sure its always0
as expected.
♻️ Add the following code and click on the Send
button to run this test, now in the Response
section click on the Test Results
tab to check if your test case passed, as seen below.
pm.test("Test response output status", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.status).to.equal(0);
});
- 3.
Test response output message
: From our API response we have amessage
variable, now lets check to make sure its alwaysSuccess
as expected.
♻️ Add the following code and click on the Send
button to run this test, now in the Response
section click on the Test Results
tab to check if your test case passed, as seen below.
pm.test("Test response output message", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.message).to.equal("Success");
});
- 4.
Test response output to be an array
: From our API response we have anoutput
variable, now lets check to make sure its alwaysan array
as expected.
♻️ Add the following code and click on the Send
button to run this test, now in the Response
section click on the Test Results
tab to check if your test case passed, as seen below.
pm.test("Test response output to be an array", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.output).to.be.an("array");
});
- 5.
Test response output to be an array which is not empty
: From our API response we have anoutput
variable, now lets check to make sure itsnot empty
as expected.
♻️ Add the following code and click on the Send
button to run this test, now in the Response
section click on the Test Results
tab to check if your test case passed, as seen below.
pm.test("Test response output to be an array which is not empty", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.output).to.not.be.empty
});
- 6.
Test response output object to have expected keys
: From our API response we have anoutput
variable and this holds some data, now lets check to make sure it has the minimumexpected data
for the first object.
♻️ Add the following code and click on the Send
button to run this test, now in the Response
section click on the Test Results
tab to check if your test case passed, as seen below.
pm.test("Test response output object to have expected keys", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.output[0]).to.have.a.property('id');
pm.expect(jsonData.output[0]).to.have.a.property('name');
pm.expect(jsonData.output[0]).to.have.a.property('post');
});
Awesome! 👏, now we are all done, I urge you to try out some more test cases in your free time, Postman offers Public APIs you can play with. Happy Postman Testing
😉.
🐱💻 Resources:
Kindly find below links to resources you can use as a guide and also links to my social media should you need to make contact for any challenges you might have or just to have a short conversation if you are starting out.
Postman: https://www.postman.com
Postman Learning: https://learning.postman.com/docs/getting-started/introduction
Postman Test Functions (ChaiJS): https://www.chaijs.com/api
GitHub repository for API code:
https://github.com/clonne101/postman-training
Social Links:
Website: https://jefferyclonne.com
Twitter: https://twitter.com/@clonne101
LinkedIn: https://www.linkedin.com/in/jeffery-osei-551626a6
Video Link:
https://dev.to/clonne101/video-postman-api-testing-2pm
Top comments (0)