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.1. Common Assertions

Interactive Audio Lesson

Session 1: Introduction to Assertions

Unlock the classroom podcast

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

Sarah
SarahInstructor

Welcome, class! Today, we will be discussing assertions and how they help in validating API responses. Can anyone tell me what they think an assertion is?

Noah
Noah

I think it's something to check if the response from the API is correct?

Sarah
SarahInstructor

Exactly, Student_1! Assertions are statements that validate whether the actual outcome matches the expected outcome. They are crucial in ensuring our APIs behave as intended.

Isabella
Isabella

What kind of things can we check with assertions?

Sarah
SarahInstructor

Great question! We can check for things like status codes, specific fields in the response, and their values. For example, we can assert that the status code we expect is 200, indicating a successful response.

Akash
Akash

So, verifying that we receive '200 OK' is a common assertion, right?

Sarah
SarahInstructor

That's right! It's fundamental. Let's remember it with the acronym 'COV' - Correct Outcome Validation. By focusing on these validations, we ensure our APIs are reliable.

Sarah
SarahInstructor

To summarize, assertions are key for validating API responses. We can check status codes and expected fields in the JSON response to make sure everything works as it should.

Session 2: Writing Assertions in Postman

Unlock the classroom podcast

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

Robert
RobertInstructor

Now that we understand what assertions are, let's talk about how to write them in Postman. Who remembers where we can write these assertions?

Noah
Noah

Is it in the 'Tests' tab after sending a request?

Robert
RobertInstructor

Correct, Student_1! The 'Tests' tab is where we write our JavaScript code to validate responses. Let's look at a simple assertion.

Isabella
Isabella

What would be an example of such an assertion?

Robert
RobertInstructor

Let's say we want to test if the response has a specific user name. We could write it like this: pm.test('Response has user name', function() { var jsonData = pm.response.json(); pm.expect(jsonData.name).to.eql('Charlie'); });

Ananya
Ananya

How does that work?

Robert
RobertInstructor

Good question, Student_4! In this code, we check if the 'name' field in our JSON response equals 'Charlie'. If it does, our test passes; if not, it fails.

Robert
RobertInstructor

Remember to use 'JSON' for parsing. To summarize, assertions in Postman can validate fields in API responses, ensuring our applications work as intended!