Catalog / Postman Testing & Debugging Cheatsheet
Postman Testing & Debugging Cheatsheet
A comprehensive cheat sheet covering Postman's testing and debugging features, including writing tests, debugging requests, and using the Postman console effectively.
Test Script Basics
Writing Tests
Tests in Postman are written in JavaScript and executed after a response is received. They use the Example:
|
The |
Assertions are written using the |
Example:
|
Accessing Response Data
|
Parses the response body as JSON. Example:
|
|
Returns the response body as a string. Example:
|
|
An object containing the response headers. Example:
|
Common Assertions
|
|
|
|
Debugging Requests
Postman Console
The Postman Console is a powerful tool for debugging requests and tests. It logs detailed information about request and response lifecycle. To open the console, click the ‘Console’ button in the bottom left corner of the Postman window. |
The console displays network information, sent headers, received headers, request body, response body, test results, and any console logs generated by your scripts. |
Use |
Example:
|
Request Information
|
The URL of the request. Example: |
|
An object containing the request headers. Example: |
|
The request body (if any). Example: |
Debugging Techniques
Use |
Check the Postman Console for error messages and warnings that can help you identify problems. |
Use the ‘Preview’ feature in the request body editor to see how Postman is interpreting your request data. |
Inspect the raw response body to ensure that the server is returning the expected data. |
Advanced Testing
Using Variables
Postman variables allow you to store and reuse values across requests and collections. Variables can be defined at different scopes: global, collection, environment, and local. |
Use |
Use |
Example:
|
Chaining Requests
Setting a variable |
|
Using a variable in another request |
|
Getting a variable |
|
Data-Driven Testing
Postman supports data-driven testing by allowing you to import data from a CSV or JSON file and use it to parameterize your requests and tests. |
Access data values using |
Example:
|
Use the Collection Runner to execute the collection multiple times, each time with a different set of data. |
Mock Servers & Contract Testing
Mock Servers
Postman Mock Servers simulate API endpoints, allowing you to develop and test your application without relying on a live API. |
Create a mock server in Postman by defining the endpoint URL, request method, headers, and response body. |
Use mock servers to test different scenarios, such as successful responses, error responses, and edge cases. |
Verify interactions with the mock server using the Postman Console. |
Contract Testing
Define API contracts |
Create a Postman Collection that defines the expected request and response schemas for your API endpoints. |
Validate responses |
Write tests in Postman to validate that the actual responses from the API match the expected schemas. |
Automate contract testing |
Use the Postman Collection Runner or Newman to automate the execution of your contract tests. |
Schema Validation
Use libraries like |
Example:
|