{"id":10521,"date":"2018-07-30T13:19:00","date_gmt":"2018-07-30T13:19:00","guid":{"rendered":"https:\/\/viewmyprojects.com\/winwirewp\/?p=10521"},"modified":"2023-11-30T05:15:24","modified_gmt":"2023-11-30T05:15:24","slug":"how-to-integrate-api-tests-with-postman-and-newman","status":"publish","type":"post","link":"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-integrate-api-tests-with-postman-and-newman\/","title":{"rendered":"How to Integrate API Tests with Postman and Newman"},"content":{"rendered":"\n<p>In the recent years, we have seen a huge surge in the development and use of Application Programming Interface (API). Today, Web applications are fueled by REST-based Web Services. APIs compress critical business logic with high SLAs. Hence it is becoming extremely crucial to test APIs as part of the&nbsp;continuous integration (CI) or continuous delivery (CD) process&nbsp;to identify bugs, reduce errors, and improve predictability.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>What is an Application Programming Interface (API)?<\/strong><\/h4>\n\n\n\n<p>An interface with a set of protocols, routines, and tools that helps in building software application and makes available to programmers to interact with other software is known as an Application Programming Interface (API), and it differs from other interfaces like User interface as below<\/p>\n\n\n\n<p>User interface communicates with a collection of windows, dialog boxes, buttons and menus whereas API consists of a set of direct calls or sofware links to lower-level functions and operations.<\/p>\n\n\n\n<p>Today, we have been watching an exponential increase in the development and use of APIs where the entire application is exposed via REST APIs which encapsulate the critical business logic of the software architecture. Hence it is vital to test APIs to determine whether it returns the correct response (in the expected format) for the given input request and also to identify the response time in an acceptable amount of time.<\/p>\n\n\n\n<p>To test the application developed based on the REST APIs, here it comes the Postman with a full-featured testing sandbox that helps us to execute the test scripts written in JavaScript language.<\/p>\n\n\n\n<p><strong>How to Install Postman on any machine<\/strong><\/p>\n\n\n\n<p>Postman can be installed either as a Chrome extension or Native application<\/p>\n\n\n\n<p>Installing as a&nbsp;<strong>Native application:<\/strong><\/p>\n\n\n\n<p>Go to apps page-&gt;Click download for Mac\/Windows\/Linux depending on platform-&gt;Run the installer<\/p>\n\n\n\n<p>Installing as a&nbsp;<strong>Chrome extension:<\/strong><\/p>\n\n\n\n<p>Go to chrome webstore-&gt;Search for Postman-&gt;Click on Add to chrome-&gt;Launch the postman<\/p>\n\n\n\n<p>Postman comes with a command line (CLI) collection runner tool called Newman. This collection runner engine sends API requests, receives the response and then runs your tests against the response. Using the Newman, the response can be achieved instantly, and collections can be exported\/imported as JSON files.<\/p>\n\n\n\n<p><strong>Postman Collections:<\/strong><\/p>\n\n\n\n<p>Postman collections is a group of saved requests that can be organized into folders. It is mostly a collection of related tests, which can be sequence based or scenario-based tests.<\/p>\n\n\n\n<p>There\u2019s a Collections tab on the top left of Postman, with an example Service swagger collection (or) to add a new collection. Click on the (+) icon below to collection tab.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"530\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-1024x530-1.webp\" alt=\"\" class=\"wp-image-18506\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-1024x530-1.webp 1024w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-1024x530-1-300x155.webp 300w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-1024x530-1-768x398.webp 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>API requests can be selected from the saved collections and assertions can be inserted in the Tests using the Java script from the code snippets present on the right pane. Postman stores the response in a global object calledresponse Body,and we can use this to access response and assert values in tests as required.<\/p>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<p>tests [\u201cResponse time:\u201d +responseTime] = responseTime;<\/p>\n\n\n\n<p>tests [\u201cStatus of Response\u201d] = responseCode.code === 200;<\/p>\n\n\n\n<p>var jsonData = JSON.parse(response Body);<\/p>\n\n\n\n<p>tests[\u201cVerify IsAutoscheduling :\u201d+ jsonData.data.isAutoSchedule] = jsonData.data.isAutoSchedule;<\/p>\n\n\n\n<p><strong>Newman Command Line (CLI) tool:<\/strong><\/p>\n\n\n\n<p>Once collections are saved and written tests for them, it may be monotonous to go through by each line and click send to see if a given collection test passed or not. This is where Newman comes in. Newman is a command-line collection runner for Postman.<\/p>\n\n\n\n<p>The next step is to export your environment variables and collection and use Newman to run tests from your terminal.<\/p>\n\n\n\n<p><strong>Installation of Newman:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Download and install NodeJS using the link \u2013<a href=\"https:\/\/nodejs.org\/download\/\" target=\"_blank\" rel=\"noopener\">https:\/\/nodejs.org\/download\/<\/a>&nbsp;and click on the 32-bit or 64-bit Windows installer package, depending on the machine configuration.<\/li>\n\n\n\n<li>Add the Node executable to your system path. Go to the Control Panel &gt; System and Security &gt; System &gt; Advanced System Settings &gt; Environment variables. Append this to the end of the PATH variable: ;C:\\Program Files\\Nodejs<\/li>\n\n\n\n<li>Open a command prompt, and type \u201cnode\u201d. The node environment should start.<\/li>\n\n\n\n<li>Type \u201cnpm install -g newman\u201d on command prompt. It should take a few minutes to install.<\/li>\n<\/ol>\n\n\n\n<p><strong>Below is the process followed to run the API Test scripts:<\/strong><\/p>\n\n\n\n<p>1. Import the Test scripts(collections) from the Postman by clicking on the Import button on Top<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"563\" height=\"63\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-API-testing.webp\" alt=\"\" class=\"wp-image-18505\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-API-testing.webp 563w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-API-testing-300x34.webp 300w\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" \/><\/figure>\n\n\n\n<p>2. Update the input requests in API test scripts if required<\/p>\n\n\n\n<p>3. Saved to the same collection and ensure Get method should follow Put method<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"384\" height=\"123\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-2.webp\" alt=\"\" class=\"wp-image-18507\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-2.webp 384w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman-2-300x96.webp 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/figure>\n\n\n\n<p>4. Export the Test scripts by clicking on Export to the physical location and save<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"282\" height=\"297\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Newman-API-testing.webp\" alt=\"\" class=\"wp-image-18509\"\/><\/figure>\n\n\n\n<p>5. Once you are in the directory, run<strong>newman run &lt;collection_name. Json&gt;,<\/strong>replacing the collection_name with the name used to save the collection.<\/p>\n\n\n\n<p>Example: newman run CalendarWorkHours.postman_collection.json<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"583\" height=\"637\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman_collection.webp\" alt=\"\" class=\"wp-image-18508\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman_collection.webp 583w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2018\/02\/Postman_collection-275x300.webp 275w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><\/figure>\n\n\n\n<p>(or)<\/p>\n\n\n\n<p>Batch file can be created by saving all the json collections in the file and saving with .bat extension and run them to execute all the collections<\/p>\n\n\n\n<p>Batch file:<\/p>\n\n\n\n<p>start newman run CalendarWorkHours.postman_collection.json<\/p>\n\n\n\n<p>start newman run CalendarDetails_8088.postman_collection.json<\/p>\n\n\n\n<p>start newman run CalendarWeekDaySlots_8088.postman_collection.json<\/p>\n\n\n\n<p>start newman run CalendarSlotsByDate_8088.postman_collection.json<\/p>\n\n\n\n<p>start newman run GetOpenSlots.postman_collection.json<\/p>\n\n\n\n<p><strong>Conclusion:<\/strong><\/p>\n\n\n\n<p>Newman and Postman comes handy for variety of test cases including suites, create usage scenarios, packs for API test cases and most importantly they can be integrated with <a href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-deploy-application-in-azure-kubernetes-service-using-azure-devops\/\">CI\/CD tools<\/a> such as Travis, Jenkins and others.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the recent years, we have seen a huge surge in the development and use of Application Programming Interface (API). Today, Web applications are fueled by REST-based Web Services. APIs compress critical business logic with high SLAs. Hence it is becoming extremely crucial to test APIs as part of the&nbsp;continuous integration (CI) or continuous delivery&hellip; <a class=\"more-link\" href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-integrate-api-tests-with-postman-and-newman\/\">Continue reading <span class=\"screen-reader-text\">How to Integrate API Tests with Postman and Newman<\/span><\/a><\/p>\n","protected":false},"author":16,"featured_media":16703,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-10521","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","entry"],"acf":[],"featured_image_src":"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test.webp","author_info":{"display_name":"Naveen","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/naveen\/"},"views":4424,"uagb_featured_image_src":{"full":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test.webp",800,440,false],"thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test-150x150.webp",150,150,true],"medium":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test-300x165.webp",300,165,true],"medium_large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test-768x422.webp",750,412,true],"large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test.webp",750,413,false],"1536x1536":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test.webp",800,440,false],"2048x2048":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test.webp",800,440,false],"post-thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/API-test.webp",800,440,false]},"uagb_author_info":{"display_name":"Naveen","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/naveen\/"},"uagb_comment_info":0,"uagb_excerpt":"In the recent years, we have seen a huge surge in the development and use of Application Programming Interface (API). Today, Web applications are fueled by REST-based Web Services. APIs compress critical business logic with high SLAs. Hence it is becoming extremely crucial to test APIs as part of the&nbsp;continuous integration (CI) or continuous delivery&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/10521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/comments?post=10521"}],"version-history":[{"count":2,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/10521\/revisions"}],"predecessor-version":[{"id":18540,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/10521\/revisions\/18540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media\/16703"}],"wp:attachment":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media?parent=10521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/categories?post=10521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/tags?post=10521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}