Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
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.
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.
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!
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'); });
In Postman's Tests, we do check, with pm.test, we keep in check.
Imagine a detective checking a case file. They look for crucial clues just like we look for status codes and fields in API tests!
Remember 'SPE' for our key test components: Status, Parse, Expect.
Use 'TAP' - Test Assertions with Postman.
Term
What function do you use to write a test in Postman?
Click to flip
Term
What does the status code 200 signify?
Click to flip
A statement that verifies if the API’s response matches expected results.
A function in Postman that creates a new test in the Tests tab.
A three-digit HTTP response code indicating the outcome of a request.
JavaScript Object Notation, a lightweight data interchange format.
This section explains how to write assertions in Postman's Test tab to validate API responses using JavaScript.
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.
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.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});pm.test("Response has user name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("Charlie");
});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.