Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
11.4. Writing Basic Assertions in Postman (Test Tab)
Learn content
Interactive Audio Lesson
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Today, we’re discussing assertions in Postman! Who can tell me what an assertion is?
Is it a statement we check against?
Exactly! Assertions let us verify that our API responses behave as expected. Can anybody give an example of what we might want to check?
We can check if the status code is 200!
Great! The status code shows whether a request was successful. Remember, if the status is 200, that's a 'success'. What’s our memory aid for that?
I remember '200 means good, the API understood!'
Perfect rhyme! To validate, we will use pm.test(). Let's look at how we can implement that in Postman.
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Now, let’s dive into writing assertions! After you send a request, you need to navigate to the Tests tab. Who knows what code we can use to check the status code?
We use pm.response.to.have.status(200)!
Correct! And we can write: pm.test('Status code is 200', function() { pm.response.to.have.status(200); }); Now, why are assertions important?
They help automate testing, right?
Exactly! Less manual work means fewer errors. Let's move on to validating response data now.
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Besides status codes, we can validate specific fields. If we expect a user's name in the JSON response, how do we do that?
We would check that the name field exists and matches what we expect.
Exactly, we use pm.expect(). For example: pm.expect(jsonData.name).to.eql("Charlie"); Can anyone summarize why we validate responses?
To make sure the API returns the correct data!
Right again! Keeping the API reliable is crucial. Practice this with different fields at home!
Overview
Short Summary
This section explains how to write assertions in Postman's Test tab to validate API responses using JavaScript.
Medium Summary
In this section, you will learn how to utilize JavaScript-based testing in Postman’s Tests tab to create assertions that validate API responses, including status codes and specific fields in the response body, thereby automating the testing process effectively.
Detailed Summary
Writing Basic Assertions in Postman (Test Tab)
Postman provides a robust platform for API testing, allowing users to write JavaScript-based assertions under the Tests tab. These assertions automate the validation process, making it easier to ensure the functionality and reliability of APIs. In this section, we focus on common assertions that can be implemented, starting with checking the HTTP status code and then verifying specific fields in the JSON response.
Key Components of Assertions
- Status Code Validation: An assertion can be written to confirm that the received status code matches the expected code, such as 200 for success.
- Response Body Validation: By parsing the JSON response, you can check whether specific fields contain the expected values, ensuring the API responds with the correct data.
Example Assertions in Postman
- Status Code Assertions: Use the following template in the Tests tab:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});- Field Value Assertions: To check if the user name in the response matches an expected value:
pm.test("Response has user name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("Charlie");
});Adding Assertions
To add an assertion, navigate to the Tests tab after sending a request and write or paste the JavaScript code to validate various aspects of the API response, such as status, headers, and the body of the response.
Assertions facilitate robust API testing and ultimately contribute to the reliability of web services.
Audio Book
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountPostman allows you to write JavaScript-based tests under the Tests tab to automate validation of response data.
Detailed Explanation
In Postman, after you perform an API request, you can validate its response using assertions. Assertions are small pieces of code written in JavaScript that check if the API response matches your expectations. This is crucial for automated testing since it helps ensure that the API works as intended without needing manual checks.
Examples & Analogies
Think of assertions like a teacher grading a student's exam. After the exam (the API response) is submitted, the teacher (the assertion) checks each answer to see if they are correct according to a set of correct answers. If they match, the student gets a passing mark.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free account🧪 Common Assertions:
// Status code is 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Response contains expected field
pm.test("Response has user name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("Charlie");
});Detailed Explanation
In this chunk, we highlight two common assertions. The first checks whether the status code of the response is 200, which indicates a successful request. The second assertion verifies that the response contains a field named 'name' with the exact value of 'Charlie'. These checks help ensure the API is functioning correctly and returning the expected results.
Examples & Analogies
Imagine you're waiting for an order at a restaurant. The status code is like the restaurant's confirmation that your order is ready (200 means it’s prepared well). The name check ensures that you actually received the right dish (in this case, the dish named 'Charlie').
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free account🛠 Where to Add: ● After sending a request, go to the Tests tab ● Write or paste JavaScript code to check status, headers, body, etc.
Detailed Explanation
After you send a request in Postman, you can navigate to the Tests tab to enter your assertions. This is the section where you write JavaScript code to verify different parts of the response, whether it be the HTTP status, response headers, or the body content. These assertions run automatically every time you send the request, providing immediate feedback on the API's performance.
Examples & Analogies
Think of the Tests tab as a checking station in a factory. Once an item (the request) passes through the production line, it goes to the checking station (Tests tab) where workers (you) verify that everything is in order, like ensuring the correct specifications (status, headers, body) are met before approval.
--
Key concepts
Core takeaways and short definitions to help you quickly recall the key ideas from this section.
- Assertions:
Statements used to validate API responses.
- pm.test():
The function to define a test in Postman.
- Status Codes:
Indicators of success or errors in API requests.
- Response Validation:
Checking expected data within the API response.
Examples
Step-by-step examples to apply the section's ideas and test your understanding.
Checking if the status code of a response is 200 using pm.test(). For example: pm.test('Status code is 200', function() { pm.response.to.have.status(200); });
Validating that the API response contains a specific user's name: pm.test('Response has user name', function() { var jsonData = pm.response.json(); pm.expect(jsonData.name).to.eql('Charlie'); });
Memory aids
Imagine a detective checking a case file. They look for crucial clues just like we look for status codes and fields in API tests!
Flash Cards
Glossary
Assertion
A statement that verifies if the API’s response matches expected results.
pm.test()
A function in Postman that creates a new test in the Tests tab.
Status Code
A three-digit HTTP response code indicating the outcome of a request.
JSON
JavaScript Object Notation, a lightweight data interchange format.