I presented this topic at "The Test Tribe Community" meet up on 27/11/2021 with a live demo.
This presentation will be a good starting point if you need to start writing simple and meaningful tests in Postman.
2. Anuradha Raman
★ Lead Automation Test Engineer
@Kissflow
★ 10+ years of experience in
software testing
★ Test Tribe champion for the
Chennai Chapter
3. Outline
➢ API Test Strategy
➢ Designing test scenarios for API testing
➢ Writing tests in Postman using different libraries
4. API Testing
Objectives:
● To ensure that the business logic implementation is working correctly as
expected
● To ensure that the implementation is working as specified according to the
requirements specification (API documentation)
● To prevent regressions between code merges and releases
5. API Testing - Scenarios
Positive Scenario
Negative Scenario
Flow Based - Series of requests which are common user actions or use
cases
Test every single API for
different sets of valid and
invalid input test data
6. Postman - A collaboration platform for
API development
● Collaborate in real-time with built-in version control
● Directly send REST, SOAP, and GraphQL requests
● Generate beautiful, machine-readable documentation
● Provide a shared context for building and consuming APIs
● Simulate endpoints and responses without having to set up a backend server
7. API Testing in Postman
a. Request level
Functional -> Request-Response Validation
Automated -> Code snippets -> Basic response code and response
object validation
a. Collection level tests
b. Schema validation test (https://ajv.js.org/)
pm.test("Response uses a valid schema", function() {
pm.expect(ajv.validate(cam, SCHEMA_STORED_AS_A_VARIABLE)).to.be.true;
});
8. Writing Tests in Postman
➔ Pre Request Tab (before)
➔ Tests (after)
➔ Assertions
◆ Parse the response body
◆ Write test script
Javascript code can be
written in both these places
9. JSON - JavaScript object notation
JSON String:
‘{
“Name”:”value”,
“array”: [ ]
}’
★ Data is in name value pairs
★ Data is separated by commas (,)
★ Curly braces holds objects
★ Square braces holds arrays
https://jsonpath.
herokuapp.com/
10. Variables
- Dynamic Variables using Faker library
(https://www.npmjs.com/package/faker)
- Generate mock/dummy test data for each run
- Some interesting dummy variables to note :
- Random filenames,
- images,
- catchphrases,
- To use in pre request and tests tab
pm.variables.replaceIn.(“{{$randomFirstname}}”)
11. Chai
Chai Assertion Library BDD: Default library in postman for assertions
This syntax provides options to optimize how readable (expect and should)
your tests are to you and your collaborators
const jsonData = pm.response.json(); -->Parsing the response body
pm.test("response data not null", function () {
pm.expect(jsonData.page).to.be.an('number');
pm.expect(jsonData.per_page).to.be.an('number');
pm.expect(jsonData.total_count).to.be.an('number');
pm.expect(jsonData.data.length).to.be.above(0);
})
Chai Assertion
12. Cheerio - Moment - Lodash
Cheerio - To parse HTML
response
Moment - working with dates Lodash - for loop
● To parse and retrieve the value
from an HTML response, we will
use the cheerio JavaScript library
● Simple library for working with the
DOM model.
● Cheerio wraps around parse5
parser and can optionally use
htmlparser2.
https://cheerio.js.org/
https://blog.logrocket.com/parsing-html-
nodejs-cheerio/
https://github.com/cheeriojs/cheerio
● To work with “dates” in Postman
● Can be used to manage test data in
pre requests tab or even query
params
https://momentjs.com/docs/
● Use with pm.expect() =>iterating
through response data, to help
assert that a certain value is
present.
● create some dynamic data to use in
our requests => The ‘_.random()’
Lodash function syntax
(_random(1000))
https://lodash.com/docs/4.17.15#forEach
13. Debugging Test Failures
● console.log(),
● console.info(),
● console.warn()
● and console.error()
=> Debug statements with test assertions,
=> Examine the content of the HTTP requests and responses, as well as Postman data
items such as variables
14. Automated tests in Postman
1. Workflows/End to End Testing/User Journey
2. Workflows=>Collection of API Requests. Automating workflows with the
collection runner
3. Default execution of requests in a Postman collection is sequential/linear
4. Configure which request to run next using pm.setNextRequest();
5. Loop over current request => Set name of current request to setNextRequest
6. Stop the run => pm.setNextRequest(null);
A basic guideline is to identify the most common parameters and conditions that an end developer will use when calling the APIs and test these scenarios extensively. Validate integration between services
Reliable Code
Need for API tests
Sweet spot of the Testing pyramid
Shift left
Agile Practices
Happy - check basic functionality and the acceptance criteria of the API
Negative - gracefully handle problem scenarios with both valid user input and invalid user input
The awesome thing about this tool is that it simplifies each step of building an API and it also makes collaboration seamless for building faster APIs.
Automate functional tests
Check API performance and response times at scheduled intervals
You can store the schema to be validated against as a collection variable and write various tests for the possible responses for an API.
The `Pre-request Script` and `Tests` tabs are basically little JavaScript IDE’s and they give us the ability to write small scripts, to assist us with things like creating test data or managing the response data that we receive from an API endpoint
Derived from Java script object notation
With JavaScript you can create an object and assign data to it, like this:
It is a common mistake to call a JSON object literal "a JSON object".
JSON cannot be an object. JSON is a string format.
You can access object values by using dot (.) notation:
You can also access object values by using bracket ([]) notation:
You access array values by index:
The sole purpose of a test is to identify that for a situation given parameters of the system are as expected. To force that the parameters of the system are correct we assert the expected values with the actual values during a test run. Assertions are used to assert that expected and actual values during a test run match. If they don't match, the test fails with the output pointing directly to the failure.
Cheerio can parse nearly any HTML or XML document.
Lodash - to check that the specific property of each object in the array
you can override this behavior using a built-in function.as the name suggests, allows you to specify which request runs next. Exit the loop with proper logic in place to avoid indefinite run