Content tests
Assert specific values in API response bodies and headers with Portman contentTests, using value, contains, oneOf, length and assert checks.
Content tests check the actual values in a response, not just its shape. Where contract tests verify the contract, tests.contentTests verify that response body properties and headers contain the values you expect.
Example
{ "version": 1.0, "tests": { "contentTests": [ { "openApiOperationId": "leadsAll", "responseBodyTests": [ { "key": "data[0].company_name", "value": "Spacex" }, { "key": "data[0].name", "contains": "Musk" }, { "key": "status_code", "oneOf": [200, 201] }, { "key": "data", "minLength": 1, "maxLength": 20 }, { "key": "@count", "notExist": true }, { "key": "service", "assert": "not.to.be.undefined" } ] }, { "openApiOperationId": "leadsOne", "responseBodyTests": [ { "key": "data.websites[*].url", "value": "http://example.com" } ], "responseHeaderTests": [ { "key": "Operation-Location", "contains": "/operations/" } ] } ] }}Targeting
Use openApiOperationId, openApiOperationIds, openApiOperation and optionally excludeForOperations. See targeting.
responseBodyTests
An array of checks on response body properties.
| Property | Type | Description |
|---|---|---|
key | string | The property to check. Use an index (data.websites[0].url) or * wildcard (data.websites[*].url, matches an object in the array using value). Use . when the body is a single value instead of a JSON object. |
value | string | The property value must equal this. |
contains | string | The property value must contain this. |
oneOf | array | The property value must be one of these strings, numbers or booleans. |
length | number | The string or array has exactly this length. |
minLength | number | The string or array has at least this length. |
maxLength | number | The string or array has at most this length. |
notExist | boolean | The key must not exist. |
assert | string | A custom Postman assertion, for example not.to.be.null. |
With only a key, the test checks that the key exists.
responseHeaderTests
An array of checks on response headers, with the same properties: key (header name), value, contains, oneOf, length, minLength, maxLength (in characters), notExist and assert.
"responseHeaderTests": [ { "key": "Operation-Location", "value": "/operations/123", "contains": "/operations/", "length": 15, "minLength": 5, "maxLength": 20, "assert": "not.to.be.undefined" }]In variations
Content tests can also be defined inside a variation, under tests.contentTests, without a target.