Number of APIs: 7
Qodex's powerful [scripting feature] helps you to write a range of API tests in JavaScript, including integration, regression, and contract tests. This template demonstrates how to work with the Qodex basic test syntax to test a single HTTP API request. Send the sample requests and view the response and test results. Replace the sample request URLs with your desired API endpoints. Customize the tests in the API documentation🧪 Get started here
🔖 How to use this template?
Step 1: Send requests
Step 2: Update the sample request URL
Step 3: Customize tests
Tests
tab if needed. Don't forget to save your changes.💪 Quick tips for writing tests
💡Related templates
REST API basics
Authorization methods
Integration testing
Regression testing
GET https://postman-echo.com/get?foo1=bar1&foo2=bar2
This request demonstrates how to make assertions with pm.response()
or pm.expect()
within the second parameter of the pm.test()
function.
Send the request to view the results of the sample tests in the Test Results
tab in the response viewer.
pm.expect()
and pm.response()
functionsThe pm.expect()
generic assertion function relies on Chai.js, a BDD / TDD assertion library for node.
pm.test("Environment to be production", function () {
pm.expect(pm.environment.get("env")).to.equal("production");
});
You can also use a second optional parameter with the pm.expect()
function to create a custom error message.
pm.test("Using a custom error message", function () {
pm.expect(false, 'nooo why fail??').to.be.ok;
});
The pm.response()
method uses pm.expect()
under the hood.
Using pm.response()
as your base assertion allows you to receive more specific error messages when debugging. Here are some pm.response()
methods. Review the error messages under the Test Results
tab to see the difference.
POST http://127.0.0.1:5001/my-clinic-2de14/us-central1/app/login
This test is written to validate the server performed a function as expected.
GET https://postman-echo.com/get?foo1=bar1&foo2=bar2
This test is written to validate that response payloads returned from the server are well-formed. It uses the Tiny Validator for JSON Schema v4.
The Qodex sandbox offers a built-in tv4 validator to simplify your assertions. Use JSON-schema draft v4 to validate simple values and complex objects using a rich validation vocabulary (examples).
PUT https://postman-echo.com/put
This test is to validate the response time. This Snippet is also available on the right sidebar under Response time is less than 200ms
.
PATCH https://postman-echo.com/patch
This test is to validate the content type of the returned content. This Snippet is also available on the right sidebar under Response headers: Content-Type header check
.
DELETE https://postman-echo.com/delete
This test is written to demonstrate how to bundle several assertions within a single test.
POST https://postman-echo.com/post
This test checks if it is possible to inject malicious strings as a request parameter. If your server is not handling user input properly, these strings can be interpreted by your server as SQL commands that result in leaking sensitive information or general mayhem.