copyright © 2021 by amundsen.com, inc. -- all rights reserved
Testing your APIs with
Postman and Newman
Mike Amundsen
@mamund
youtube.com/mamund
copyright © 2021 by amundsen.com, inc. -- all rights reserved
2
copyright © 2021 by amundsen.com, inc. -- all rights reserved
"From design to code to test to
deployment, unlock hidden business value
and release stable and scalable web APIs
that meet customer needs and solve
important business problems in a
consistent and reliable manner."
-- Pragmatic Publishers
g.mamund.com/GreatWebAPIs
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Overview
● The Goals of API Testing
● Simple Request Testing with SRTs
● Testing with Postman UI and ChaiJS Assertions
● Automate Testing with Newman CLI
● Summary
copyright © 2021 by amundsen.com, inc. -- all rights reserved
The Goals of API Testing
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Goals of API Testing
● "Outside-in" approach
● Validating resource inputs/outputs
● Confirming interface behavior
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Goals of API Testing
● "Outside-in" approach
○ We can only "see" the API
○ Your tests are for the interface, not the code
● Validating resource inputs/outputs
● Confirming interface behavior
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Goals of API Testing
● "Outside-in" approach
● Validating resource inputs/outputs
○ Confirm the URL exists, responds
○ Validate the methods, parameters, and response bodies
● Confirming interface behavior
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Goals of API Testing
● "Outside-in" approach
● Validating resource inputs/outputs
● Confirming interface behavior
○ Validate expected behaviors
○ Happy-path tests (200 OK)
○ Sad-path tests (400 Bad Request)
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Let's see how that works...
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Simple Request Testing with SRTs
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Simple Request Testing (SRTs)
● SRTs are a great way to validate endpoints
● Use curl to execute simple requests against your API
● "Eyeball" the results to make sure it works as expected
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Simple Request Testing (SRTs)
● SRTs are a great way to validate endpoints
○ Confirm URLs respond
○ Validate 200 OK & body
● Use curl to execute simple requests against your API
● "Eyeball" the results to make sure it works as expected
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Simple Request Testing (SRTs)
● SRTs are a great way to validate endpoints
● Use curl to execute simple requests against your API
○ curl or wget or any other simple HTTP CLI client works fine
○ Pipe results to a file (output.txt) and check into source control
● "Eyeball" the results to make sure it works as expected
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Simple Request Testing (SRTs)
● SRTs are a great way to validate endpoints
● Use curl to execute simple requests against your API
● "Eyeball" the results to make sure it works as expected
○ Does the request fail?
○ Does the body "look ok?"
copyright © 2021 by amundsen.com, inc. -- all rights reserved
SRT Demo
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Testing with Postman UI and ChaiJS
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Postman UI and ChaiJS
● Postman API platform (2014)
● Collections -> Folders -> Requests
● Embedded libraries for scripting tests
● ChaiJS assertions
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Postman UI and ChaiJS
● Postman API platform (2014)
○ Design, build, mock, test, etc.
○ May require a client-side install
● Collections -> Folders -> Requests
● Embedded libraries for scripting tests
● ChaiJS assertions
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Postman UI and ChaiJS
● Postman API platform (2014)
● Collections -> Folders -> Requests
○ You can import/export collections
○ Supports global, collection, and environment memory sharing
● Embedded libraries for scripting tests
● ChaiJS assertions
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Postman UI and ChaiJS
● Postman API platform (2014)
● Collections -> Folders -> Requests
● Embedded libraries for scripting tests
○ 12+ client-side libraries
○ 10+ NodeJS libraries
● ChaiJS assertions
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Postman UI and ChaiJS
● Postman API platform (2014)
● Collections -> Folders -> Requests
● Embedded libraries for scripting tests
● ChaiJS assertions
○ BDD-style assertions
○ Fluent interface
○ should(...), expect(...), assert(...)
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Postman UI Demo
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Automate Testing with Newman CLI
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Automate Testing with Newman CLI
● Newman (2016) is the command-line client for Postman
● Use Postman API for managing collections & environments
● Newman + API = custom test-run scripts
● Add -reporters option for improved output
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Automate Testing with Newman CLI
● Newman is the command-line client for Postman (2016)
○ npm install -g newman
○ newman run <collection>
● Postman API for managing collections & environments
● Newman + API = custom test-run scripts
● Add -reporters option for improved output
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Automate Testing with Newman CLI
● Newman is the command-line client for Postman (2016)
● Postman API for managing collections & environments
○ Generate your API key
○ Use curl to make postman API calls for collections & environments
● Newman + API = custom test-run scripts
● Add -reporters option for improved output
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Automate Testing with Newman CLI
● Newman is the command-line client for Postman (2016)
● Postman API for managing collections & environments
● Newman + API = custom test-run scripts
○ Pull proper collections/environments (API)
○ Run tests and report results (Newman)
○ test-run.sh local
● Add -reporters option for improved output
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Automate Testing with Newman CLI
● Newman is the command-line client for Postman (2016)
● Postman API for managing collections & environments
● Newman + API = custom test-run scripts
● Add -reporters option for improved output
○ npm install -g newman-reporter-htmlextra
○ newman run <collection> -r htmlextra
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Newman CLI Demo
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Putting it all together
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Putting It All Together w/ a "mini-pipeline"
● Build
● Local Test
● Deploy
● Remote Test
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Putting It All Together w/ a "mini-pipeline"
● Build
○ npm run dev
● Local Test
○ test-run.sh local
● Deploy
○ git push heroku master
● Remote Test
○ test-run.sh remote
copyright © 2021 by amundsen.com, inc. -- all rights reserved
mini-pipeline Demo
copyright © 2021 by amundsen.com, inc. -- all rights reserved
And So...
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Goals of API Testing
● Outside-in approach
● Testing the API's behavior
● Happy- and Sad-Path testing
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Validate Endpoints with SRTs
● Focus on URLs and arguments
● Use the srt.sh script with curl
● That's really "testing" yet <g>
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Test Behavior with Postman and ChaiJS
● Create Postman test collections
● Use ChaiJS assertion library BDD-style "outside-in"
● Build up reusable test modules and share via global memory
● Be sure to use both Happy- and Sad-Path tests
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Automate Testing with Newman CLI
● Newman is the Postman CLI client
● Automate testing using test-run.sh
● Optionally, use -r htmlextra option for HTML output
copyright © 2021 by amundsen.com, inc. -- all rights reserved
That's all there is!
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Resources
● "Design and Build Great Web APIs"
g.mamund.com/greatwebapis
● This talk (slides, links, etc.)
g.mamund.com/2021-02-postman-galaxy-testing
copyright © 2021 by amundsen.com, inc. -- all rights reserved
Testing your APIs with
Postman and Newman
Mike Amundsen
@mamund
youtube.com/mamund

Testing Your APIs: Postman, Newman, and Beyond

  • 1.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Testing your APIs with Postman and Newman Mike Amundsen @mamund youtube.com/mamund
  • 2.
    copyright © 2021by amundsen.com, inc. -- all rights reserved 2
  • 3.
    copyright © 2021by amundsen.com, inc. -- all rights reserved "From design to code to test to deployment, unlock hidden business value and release stable and scalable web APIs that meet customer needs and solve important business problems in a consistent and reliable manner." -- Pragmatic Publishers g.mamund.com/GreatWebAPIs
  • 4.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Overview ● The Goals of API Testing ● Simple Request Testing with SRTs ● Testing with Postman UI and ChaiJS Assertions ● Automate Testing with Newman CLI ● Summary
  • 5.
    copyright © 2021by amundsen.com, inc. -- all rights reserved The Goals of API Testing
  • 6.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Goals of API Testing ● "Outside-in" approach ● Validating resource inputs/outputs ● Confirming interface behavior
  • 7.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Goals of API Testing ● "Outside-in" approach ○ We can only "see" the API ○ Your tests are for the interface, not the code ● Validating resource inputs/outputs ● Confirming interface behavior
  • 8.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Goals of API Testing ● "Outside-in" approach ● Validating resource inputs/outputs ○ Confirm the URL exists, responds ○ Validate the methods, parameters, and response bodies ● Confirming interface behavior
  • 9.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Goals of API Testing ● "Outside-in" approach ● Validating resource inputs/outputs ● Confirming interface behavior ○ Validate expected behaviors ○ Happy-path tests (200 OK) ○ Sad-path tests (400 Bad Request)
  • 10.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Let's see how that works...
  • 11.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Simple Request Testing with SRTs
  • 12.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Simple Request Testing (SRTs) ● SRTs are a great way to validate endpoints ● Use curl to execute simple requests against your API ● "Eyeball" the results to make sure it works as expected
  • 13.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Simple Request Testing (SRTs) ● SRTs are a great way to validate endpoints ○ Confirm URLs respond ○ Validate 200 OK & body ● Use curl to execute simple requests against your API ● "Eyeball" the results to make sure it works as expected
  • 14.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Simple Request Testing (SRTs) ● SRTs are a great way to validate endpoints ● Use curl to execute simple requests against your API ○ curl or wget or any other simple HTTP CLI client works fine ○ Pipe results to a file (output.txt) and check into source control ● "Eyeball" the results to make sure it works as expected
  • 15.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Simple Request Testing (SRTs) ● SRTs are a great way to validate endpoints ● Use curl to execute simple requests against your API ● "Eyeball" the results to make sure it works as expected ○ Does the request fail? ○ Does the body "look ok?"
  • 16.
    copyright © 2021by amundsen.com, inc. -- all rights reserved SRT Demo
  • 17.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Testing with Postman UI and ChaiJS
  • 18.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Postman UI and ChaiJS ● Postman API platform (2014) ● Collections -> Folders -> Requests ● Embedded libraries for scripting tests ● ChaiJS assertions
  • 19.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Postman UI and ChaiJS ● Postman API platform (2014) ○ Design, build, mock, test, etc. ○ May require a client-side install ● Collections -> Folders -> Requests ● Embedded libraries for scripting tests ● ChaiJS assertions
  • 20.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Postman UI and ChaiJS ● Postman API platform (2014) ● Collections -> Folders -> Requests ○ You can import/export collections ○ Supports global, collection, and environment memory sharing ● Embedded libraries for scripting tests ● ChaiJS assertions
  • 21.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Postman UI and ChaiJS ● Postman API platform (2014) ● Collections -> Folders -> Requests ● Embedded libraries for scripting tests ○ 12+ client-side libraries ○ 10+ NodeJS libraries ● ChaiJS assertions
  • 22.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Postman UI and ChaiJS ● Postman API platform (2014) ● Collections -> Folders -> Requests ● Embedded libraries for scripting tests ● ChaiJS assertions ○ BDD-style assertions ○ Fluent interface ○ should(...), expect(...), assert(...)
  • 23.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Postman UI Demo
  • 24.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Automate Testing with Newman CLI
  • 25.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Automate Testing with Newman CLI ● Newman (2016) is the command-line client for Postman ● Use Postman API for managing collections & environments ● Newman + API = custom test-run scripts ● Add -reporters option for improved output
  • 26.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Automate Testing with Newman CLI ● Newman is the command-line client for Postman (2016) ○ npm install -g newman ○ newman run <collection> ● Postman API for managing collections & environments ● Newman + API = custom test-run scripts ● Add -reporters option for improved output
  • 27.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Automate Testing with Newman CLI ● Newman is the command-line client for Postman (2016) ● Postman API for managing collections & environments ○ Generate your API key ○ Use curl to make postman API calls for collections & environments ● Newman + API = custom test-run scripts ● Add -reporters option for improved output
  • 28.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Automate Testing with Newman CLI ● Newman is the command-line client for Postman (2016) ● Postman API for managing collections & environments ● Newman + API = custom test-run scripts ○ Pull proper collections/environments (API) ○ Run tests and report results (Newman) ○ test-run.sh local ● Add -reporters option for improved output
  • 29.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Automate Testing with Newman CLI ● Newman is the command-line client for Postman (2016) ● Postman API for managing collections & environments ● Newman + API = custom test-run scripts ● Add -reporters option for improved output ○ npm install -g newman-reporter-htmlextra ○ newman run <collection> -r htmlextra
  • 30.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Newman CLI Demo
  • 31.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Putting it all together
  • 32.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Putting It All Together w/ a "mini-pipeline" ● Build ● Local Test ● Deploy ● Remote Test
  • 33.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Putting It All Together w/ a "mini-pipeline" ● Build ○ npm run dev ● Local Test ○ test-run.sh local ● Deploy ○ git push heroku master ● Remote Test ○ test-run.sh remote
  • 34.
    copyright © 2021by amundsen.com, inc. -- all rights reserved mini-pipeline Demo
  • 35.
    copyright © 2021by amundsen.com, inc. -- all rights reserved And So...
  • 36.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Goals of API Testing ● Outside-in approach ● Testing the API's behavior ● Happy- and Sad-Path testing
  • 37.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Validate Endpoints with SRTs ● Focus on URLs and arguments ● Use the srt.sh script with curl ● That's really "testing" yet <g>
  • 38.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Test Behavior with Postman and ChaiJS ● Create Postman test collections ● Use ChaiJS assertion library BDD-style "outside-in" ● Build up reusable test modules and share via global memory ● Be sure to use both Happy- and Sad-Path tests
  • 39.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Automate Testing with Newman CLI ● Newman is the Postman CLI client ● Automate testing using test-run.sh ● Optionally, use -r htmlextra option for HTML output
  • 40.
    copyright © 2021by amundsen.com, inc. -- all rights reserved That's all there is!
  • 41.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Resources ● "Design and Build Great Web APIs" g.mamund.com/greatwebapis ● This talk (slides, links, etc.) g.mamund.com/2021-02-postman-galaxy-testing
  • 42.
    copyright © 2021by amundsen.com, inc. -- all rights reserved Testing your APIs with Postman and Newman Mike Amundsen @mamund youtube.com/mamund