Common Assertion Examples
Executing Assertions via Post-Execution Scripts
Open an interface debugging interface in EchoAPI, select the post-execution script, and enter the following JavaScript code:
// Check the response code returned by the API. If the response code is 200, it passes; otherwise, it fails.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
This code uses the pm library to run the test method. The function within the code represents an assertion, and the result of the assertion can be viewed in the response area under Assertions & Validations.
EchoAPI uses the Chai Assertion Library's BDD syntax. The chainable .to.have syntax makes it easier for developers to write and understand assertions.
After clicking "Send", check the assertion results in the Test results
section on the side of the response area.
To understand the conditions when an interface passes or fails, you can change the status code in the assertion code and click "Send" again, as follows:
// Check the response code returned by the API. If the response code is 201, it passes; otherwise, it fails.
pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
You can build assertions in multiple ways. The following code shows another method using expect to achieve the same assertion:
pm.test("Status code is 200", () => {
pm.expect(pm.response.code).to.eql(200);
});
For a complete overview of assertion syntax options, please refer to the Chai Assertion Library Documentation.
Using Multiple Assertions
In the post-execution script, you can add multiple assertions combined to verify the interface.
pm.test("The response has all properties", () => {
// Parse the response JSON and validate three properties
const responseJson = pm.response.json();
pm.expect(responseJson.type).to.eql('vip');
pm.expect(responseJson.name).to.be.a('string');
pm.expect(responseJson.id).to.have.lengthOf(1);
});
If any of the assertions fail, the entire validation will fail. All assertions must pass for the test to succeed.
Parsing Response Body Data
To perform assertion validations on the response data, you should first parse the data into a JavaScript object that can be used in the assertions.
Parse JSON data with the following code:
const responseJson = pm.response.json();
To parse XML, use the following code:
const responseJson = xml2Json(pm.response.text());
To parse CSV, use the CSV parsing utility:
const parse = require('csv-parse/lib/sync');
const responseJson = parse(pm.response.text());
To parse HTML, use Cheerio:
const $ = cheerio.load(pm.response.text());
// Print the test HTML
console.log($.html());
Handling Unparsable Responses
If you cannot parse the response result into JavaScript, and its format is not JSON, XML, HTML, CSV, or any other parsable data format, you can also make assertions on it.
Validate if the response body contains a string:
pm.test("Body contains string",() => {
pm.expect(pm.response.text()).to.include("customer_id");
});
This assertion doesn't specify the location of the string in the response, as it checks the entire response body. Validate if the response matches a string (usually only effective for short responses):
pm.test("Body is string", function () {
pm.response.to.have.body("whole-body-text");
});
Making Assertions on HTTP Responses
Validate various aspects of the request response, including body, status codes, headers, cookies, response times, etc.
Testing the Response Body
Check for specific values in the response body:
pm.test("Person is Jane", () => {
const responseJson = pm.response.json();
pm.expect(responseJson.name).to.eql("Jane");
pm.expect(responseJson.age).to.eql(23);
});
Testing Status Codes
Test the response status code:
pm.test("Status code is 201", () => {
pm.response.to.have.status(201);
});
If you want to validate whether the status code is one of a group of status codes, include them all in an array and use oneOf
:
pm.test("Successful POST request", () => {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
Check the status code text:
pm.test("Status code name has string", () => {
pm.response.to.have.status("Created");
});
Testing Headers
Check if a response header exists:
pm.test("Content-Type header is present", () => {
pm.response.to.have.header("Content-Type");
});
Test for a response header with a specific value:
pm.test("Content-Type header is application/json", () => {
pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json');
});
Testing Cookies
Test if a cookie exists in the response:
pm.test("Cookie JSESSIONID is present", () => {
pm.expect(pm.cookies.has('JSESSIONID')).to.be.true;
});
Test a specific cookie value:
pm.test("Cookie isLoggedIn has value 1", () => {
pm.expect(pm.cookies.get('isLoggedIn')).to.eql('1');
});
Testing Response Time
Test if the response time is within a specified range:
pm.test("Response time is less than 200ms", () => {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Common Assertion Examples
Below are common assertion examples that you may find useful in scripts. You can execute them as provided or modify them to suit your specific needs
For a more comprehensive overview of what can be included in assertions, please refer to the Chai Assertion Library Documentation.
Asserting Response Values for Variables
Check if the response property has the same value as a variable (in this case, an environment variable):
pm.test("Response property matches environment variable", function () {
pm.expect(pm.response.json().name).to.eql(pm.environment.get("name"));
});
Validating Value Types
Validate the type of any part of the response:
/* The response content has the following structure:
{
"name": "Jane",
"age": 29,
"hobbies": [
"skating",
"painting"
],
"email": null
}
*/
const jsonData = pm.response.json();
pm.test("Test data type of the response", () => {
pm.expect(jsonData).to.be.an("object");
pm.expect(jsonData.name).to.be.a("string");
pm.expect(jsonData.age).to.be.a("number");
pm.expect(jsonData.hobbies).to.be.an("array");
pm.expect(jsonData.website).to.be.undefined;
pm.expect(jsonData.email).to.be.null;
});
Validating Array Properties
Check if the array is empty and if it contains specific items:
/*
The response content has the following structure:
{
"errors": [],
"areas": [ "goods", "services" ],
"settings": [
{
"type": "notification",
"detail": [ "email", "sms" ]
},
{
"type": "visual",
"detail": [ "light", "large" ]
}
]
}
*/
const jsonData = pm.response.json();
pm.test("Test array properties", () => {
// Errors data is empty
pm.expect(jsonData.errors).to.be.empty;
// Areas include "goods"
pm.expect(jsonData.areas).to.include("goods");
// Get the notification settings object
const notificationSettings = jsonData.settings.find
(m => m.type === "notification
");
pm.expect(notificationSettings)
.to.be.an("object", "Could not find the setting");
// The detail array must include "sms"
pm.expect(notificationSettings.detail).to.include("sms");
// The detail array must include all listed elements
pm.expect(notificationSettings.detail)
.to.have.members(["email", "sms"]);
});
The order of .members
does not impact the test outcome.
Validating Object Properties
Verify that an object contains specific keys or properties:
pm.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
pm.expect({a: 1}).to.have.property('a');
pm.expect({a: 1, b: 2}).to.be.an('object')
.that.has.all.keys('a', 'b');
The target can be an object, set, array, or map. If you run it without specifying .keys
, the expression defaults to .all
. The behavior may vary depending on the target, so it is advisable to review the context before using modifiers like .with
, .all
, or .any.
Validating That a Value Is in a Collection
Check the response value against a list of valid options:
pm.test("Value is in valid list", () => {
pm.expect(pm.response.json().type)
.to.be.oneOf(["Subscriber", "Customer", "User"]);
});
Validating That an Object Is Contained
Check if the object is part of the parent object:
/*
The response has the following structure:
{
"id": "d8893057-3e91-4cdd-a36f-a0af460b6373",
"created": true,
"errors": []
}
*/
pm.test("Object is contained", () => {
const expectedObject = {
"created": true,
"errors": []
};
pm.expect(pm.response.json()).to.deep.include(expectedObject);
});
Using .deep
in assertions like .equal
, .include
, .members
, .keys
, and .PROPERTY
enforces deep equality (loose equality) instead of strict equality (===
). While .eql
also applies loose equality, .deep
.equal
ensures that all subsequent assertions in the chain adopt deep equality, unlike .eql
, which applies it only to the specific comparison.
Validating the Current Environment
Check the currently selected environment:
pm.test("Check the active environment", () => {
pm.expect(pm.environment.name).to.eql("Production");
});
Common Test Error Troubleshooting
When errors or unexpected behavior arise in scripts, the console can be a useful tool for troubleshooting. By using console.log()
, console.info()
, and debug statements alongside assertions, you can examine the contents of HTTP requests, responses, and EchoAPI data (e.g., variables). Additional commands like console.warn()
, console.error()
, and console.clear()
can further assist in debugging.
Print the values of variables or response properties:
console.log(pm.collectionVariables.get("name"));
console.log(pm.response.json().name);
Print the types of variables or response properties:
console.log(typeof pm.response.json().id);
Use console logs to mark code execution, sometimes called "trace statements":
if (pm.response.json().id) {
console.log("id was found!");
// Perform some action
} else {
console.log("no id ...");
// Perform another action
}
Validating Deep Equality Errors
You may encounter AssertionError: expected <value> to deeply equal '<value>'.
For example, the following code results in this error:
pm.expect(1).to.eql("1");
This happens because the assertion validation is comparing a number to a string value. The validation will only return true
when the type and value are equal.
JSON Undefined Error
You may encounter the ReferenceError: jsonData is not defined
issue. This usually occurs when you try to reference a JSON object that has not been declared or is out of the scope of the assertion validation code.
pm.test("Test 1", () => {
const jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("John");
});
pm.test("Test 2", () => {
pm.expect(jsonData.age).to.eql(29); // jsonData is not defined
});
Ensure the code assigning the response data to a variable is accessible to all assertion code. For example, moving const jsonData = pm.response.json();
before the first pm.test
makes it available in both assertion functions.
Validating Undefined Errors
You may encounter the AssertionError: expected undefined to deeply equal..
issue. This usually happens when referencing a non-existent or out-of-scope property.
pm.expect(jsonData.name).to.eql("John");
In this example, if you receive the error message AssertionError: expected undefined to deeply equal 'John'
, it means that the name
property is not defined in the jsonData
object.
Validating Failures
Sometimes you may expect a validation to fail, but it does not.
// The assertion function is not correctly defined - the second argument is missing.
pm.test("Not failing", function () {
pm.expect(true).to.eql(false);
});
Make sure your test code is syntactically correct before sending the request.
Validating Response Structures
Perform JSON schema validation using Tiny Validator V4 (tv4):
const schema = {
"items": {
"type": "boolean"
}
};
const data1 = [true, false];
const data2 = [true, 123];
pm.test('Schema is valid', function() {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});
Validate JSON schema using Ajv JSON schema validator:
const schema = {
"properties": {
"alpha": {
"type": "boolean"
}
}
};
pm.test('Schema is valid', function() {
pm.response.to.have.jsonSchema(schema);
});
Sending Asynchronous Requests
Send a request in the post-execution script and log the response:
pm.sendRequest("https://httpbin.org/anything", function (err, response) {
console.log(response.json());
});