AllRounder.ai
Chapters in this course

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.

Enrol free

11.4. Writing Basic Assertions in Postman (Test Tab)

Interactive Audio Lesson

Session 1: Introduction to Assertions in Postman

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

Today, we’re discussing assertions in Postman! Who can tell me what an assertion is?

Noah
Noah

Is it a statement we check against?

Sarah
SarahInstructor

Exactly! Assertions let us verify that our API responses behave as expected. Can anybody give an example of what we might want to check?

Isabella
Isabella

We can check if the status code is 200!

Sarah
SarahInstructor

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?

Akash
Akash

I remember '200 means good, the API understood!'

Sarah
SarahInstructor

Perfect rhyme! To validate, we will use pm.test(). Let's look at how we can implement that in Postman.

Session 2: Writing Assertions

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Robert
RobertInstructor

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?

Ananya
Ananya

We use pm.response.to.have.status(200)!

Robert
RobertInstructor

Correct! And we can write: pm.test('Status code is 200', function() { pm.response.to.have.status(200); }); Now, why are assertions important?

Isabella
Isabella

They help automate testing, right?

Robert
RobertInstructor

Exactly! Less manual work means fewer errors. Let's move on to validating response data now.

Session 3: Understanding Response Validation

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

Besides status codes, we can validate specific fields. If we expect a user's name in the JSON response, how do we do that?

Noah
Noah

We would check that the name field exists and matches what we expect.

Sarah
SarahInstructor

Exactly, we use pm.expect(). For example: pm.expect(jsonData.name).to.eql("Charlie"); Can anyone summarize why we validate responses?

Akash
Akash

To make sure the API returns the correct data!

Sarah
SarahInstructor

Right again! Keeping the API reliable is crucial. Practice this with different fields at home!