<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Carles Kapy</title>
    <description>The latest articles on DEV Community by Carles Kapy (@carleskapy).</description>
    <link>https://dev.to/carleskapy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F9451%2FQws-zICs.jpeg</url>
      <title>DEV Community: Carles Kapy</title>
      <link>https://dev.to/carleskapy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/carleskapy"/>
    <language>en</language>
    <item>
      <title>Automate your API tests with Jenkins, Postman and Newman</title>
      <dc:creator>Carles Kapy</dc:creator>
      <pubDate>Wed, 02 Jan 2019 21:28:14 +0000</pubDate>
      <link>https://dev.to/carleskapy/automate-your-api-tests-with-jenkins-postman-and-newman-3079</link>
      <guid>https://dev.to/carleskapy/automate-your-api-tests-with-jenkins-postman-and-newman-3079</guid>
      <description>&lt;h2&gt;
  
  
  A little background
&lt;/h2&gt;

&lt;p&gt;Nowadays CI pipelines are a must on our development dayjob, helping us to shorten delivery cycles and to bring higher quality software.&lt;/p&gt;

&lt;p&gt;As a backend developer, APIs have became a cornestone on most of my applications, so testing them became a must.&lt;/p&gt;

&lt;p&gt;Having an API Test Stage in our CI pipeline brings us confidence in the quality of the product and alerts us if something had broken.&lt;/p&gt;

&lt;h2&gt;
  
  
  Postman
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.getpostman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; is a great tool that helps developers in every stage of our API lifecycle. It has a great &lt;a href="https://learning.getpostman.com/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and an increasingly community of developers which use it, so it is easy to find posts explaining cool tips and tricks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collections
&lt;/h3&gt;

&lt;p&gt;Postman makes easy to organize our different request into &lt;a href="https://learning.getpostman.com/docs/postman/collections/intro_to_collections" rel="noopener noreferrer"&gt;collections&lt;/a&gt;. Personally I create a collection for every project I want to test. Then, inside the collection, I create a folder for every entity on the project, trying to cover every endpoint in the API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qwGqRlyN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/1v967smnl4xen35arw2k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qwGqRlyN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/1v967smnl4xen35arw2k.png" alt="Requests organized by collections" width="407" height="365"&gt;&lt;/a&gt;&lt;br&gt;Requests are organized by entities
  &lt;/p&gt;

&lt;h3&gt;
  
  
  Environments
&lt;/h3&gt;

&lt;p&gt;Postman allows us to create different &lt;a href="https://learning.getpostman.com/docs/postman/environments_and_globals/intro_to_environments_and_globals" rel="noopener noreferrer"&gt;environments&lt;/a&gt; to run our requests. Giving a real example, I would tell you I work on three different environments: my local environment, a staging environment and a production environment, so I  create three different environments with variables which represents the changing data between the environments (like the base path for the API requests). This way I can define a single request but target it to my three different environments easily. Think about how easy would be to change or add a new environment!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l-6Oe2at--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/x0fxp2f4xj7in72uw7ge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l-6Oe2at--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/x0fxp2f4xj7in72uw7ge.png" alt="Using environment variables on request url" width="800" height="133"&gt;&lt;/a&gt;&lt;br&gt;Extracting base path as environment variable to target different environments
  &lt;/p&gt;

&lt;h3&gt;
  
  
  Testing requests
&lt;/h3&gt;

&lt;p&gt;One of the greatest features of Postman is that allows you to code &lt;a href="https://learning.getpostman.com/docs/postman/scripts/test_scripts" rel="noopener noreferrer"&gt;tests&lt;/a&gt; that runs every time a request run. They are written in JS and have a cool set of assertions to check things like the response code, response body o response headers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ClVgtXna--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/r3zotdaf4r0x96tjwyu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ClVgtXna--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/r3zotdaf4r0x96tjwyu1.png" alt="A request with few tests" width="800" height="544"&gt;&lt;/a&gt;&lt;br&gt;A request with few tests
  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sweet!&lt;/em&gt; Once we have written a test suite to cover our API, we use the &lt;a href="https://learning.getpostman.com/docs/postman/collection_runs/starting_a_collection_run/" rel="noopener noreferrer"&gt;Collection Runner&lt;/a&gt;, a built-in tool provided by Postman to run every request of a collection and output a report of tests results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YwiiBDuM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/dljz69xvs7tm28mdt34g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YwiiBDuM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/dljz69xvs7tm28mdt34g.png" alt="Collection runner test report" width="800" height="501"&gt;&lt;/a&gt;&lt;br&gt;How beatiful are the green meadows...
  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bonus: Sometimes, we want to run our requests in a certain order and guess what? Postman allows us to do it! Check it at &lt;a href="https://learning.getpostman.com/docs/postman/collection_runs/building_workflows/" rel="noopener noreferrer"&gt;building workflows issue&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What do we have so far?
&lt;/h3&gt;

&lt;p&gt;At this point, we have build up a Postman collection with a bunch of request that could target our API in all our environments. Besides that, every request has several tests to check if it is working properly. &lt;em&gt;What a time to be alive!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Newman
&lt;/h2&gt;

&lt;p&gt;Do you remember the Collection Runner I mentioned previously? It could run every request in our collection with a simple click. But what about a command line tool to do the same? &lt;a href="https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/" rel="noopener noreferrer"&gt;Newman&lt;/a&gt; does it. Newman is a command line Collection Runner for Postman that allows you to run and test a Postman Collection directly from the command line and integrate it easily in CI servers, that makes it a great tool for our purpose: &lt;strong&gt;Automate our API test with Jenkins&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Run a collection
&lt;/h3&gt;

&lt;p&gt;In order to run a collection with Newman, we first have to export it from Postman. Since we have some variables playing around in some of our request, we have to export the suitable environment too. Once we have both files, we are able to run the collection with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;newman run our.postman_collection.json &lt;span class="nt"&gt;-e&lt;/span&gt; our.postman_environment.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and we will see a report of the collection run, similar to others testing frameworks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3_BYtGi0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/bhfe61yojs428khstmqj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3_BYtGi0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/bhfe61yojs428khstmqj.PNG" alt="Newman CLI test report" width="470" height="574"&gt;&lt;/a&gt;&lt;br&gt;Newman CLI test report
  &lt;/p&gt;

&lt;h3&gt;
  
  
  Formatting the output
&lt;/h3&gt;

&lt;p&gt;The report can be formatted in different ways. To achieve this, Newman provides a set of &lt;strong&gt;reporters&lt;/strong&gt; to support different output formats like JSON, XUNIT or HTML. Besides that you can create your own reporter to format the report in any format you want.&lt;/p&gt;

&lt;h2&gt;
  
  
  Jenkins
&lt;/h2&gt;

&lt;p&gt;Cool, we have defined our &lt;em&gt;API test suite&lt;/em&gt; with Postman and we are able to run it from command line and obtain a nice report of the results so far with Newman. But now we want to include our awesome new skill in our CI pipeline.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: We are using Jenkins as an automation server to build up our CI/CD pipelines and we run our CI pipeline in a Docker container which have all the necessary tools installed (such Newman).&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Mixing things up
&lt;/h3&gt;

&lt;p&gt;Essentially we will need three files to include our API Test inside a CI pipeline:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Jenkinsfile&lt;/em&gt;, which defines our pipeline&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Postman Collection&lt;/em&gt;, which defines our set of request and tests&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Postman Environment&lt;/em&gt;, which defines the variables of the target environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We include all of them into project repository, so we can benefit of version control over them and track their changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enough talking, let’s code!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We need to create a new stage in our pipeline to run the API Test. We usually put it after unit and integration tests to be sure everything is running as expected before testing the interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pipeline&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nf"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Test API Rest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;steps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;sh&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newman run tests/Newman/our.postman_collection.json -e tests/Newman/env/test.postman_environment.json -r junit,html --reporter-junit-export var/reports/newman/junit/newman.xml --reporter-html-export var/reports/newman/html/index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

            &lt;span class="nf"&gt;publishHTML&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;allowMissing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;alwaysLinkToLastBuild&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;keepAll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reportDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;var/reports/newman/html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reportFiles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reportName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Newman API Test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reportTitles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we invoke the run command of Newman, providing our collection an the test environment file as parameters. Besides that, we also inform which reporters we want to use (JUNIT, HTML) and where the outputs will be stored.&lt;/p&gt;

&lt;p&gt;As a bonus, we use &lt;a href="https://wiki.jenkins.io/display/JENKINS/HTML+Publisher+Plugin" rel="noopener noreferrer"&gt;HTML PUblisher Jenkins plugin&lt;/a&gt; to link the HTML report to the build.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this journey as much as I did writing it and, as always, feedback will be welcomed!&lt;/p&gt;

</description>
      <category>jenkins</category>
      <category>testing</category>
      <category>postman</category>
      <category>qa</category>
    </item>
  </channel>
</rss>
