Skip to main content Skip to complementary content

Assertion operators

Use Talend Cloud API Tester's assertion operators.

Assertions rely on operators. Talend Cloud API Tester has a set of operators that you can use.

Information noteNote: If you want a response body to be interpreted as JSON, you need to use the JSON body element in your assertion. If you use the Body content element, the body will be interpreted as a string.

Equality

The operators Equals and Does not equal are the simplest of all and only do an equality check. Fortunately, they do understand JSON, meaning that if used on JSON arrays, they will not invalidate the request if the expected and actual arrays are formatted differently.

The operator Does not equal is the exact opposite of Equals.

Comparison

The comparison operators are:

  • Greater than
  • Greater than or equal
  • Less than
  • Less than or equal

Comparisons are done this way:

  • On numbers, it does a number comparison.
  • On strings, it compares the string lengths.
  • On booleans, JSON arrays and JSON objects, it compares them as strings.
  • On null, an error is displayed.

The objects that you compare must have the same format. To avoid issues related to formats, you can first use an assertion to validate the format of the objects you want to compare, using a regex for example.

Date comparisons:
  • Dates in the timestamp format are handled as integers and dates in the ISO format are handled as strings.
  • ISO dates are compared lexicographically and the comparison can only be performed accurately if they use the same format and time zone. Talend Cloud API Tester produces dates in the ISO format with the UTC time zone.
  • You can get false positive results if the date is in a different format or if you are comparing a date with another type of value. For example, if you are checking that a date is lesser than another one, a date in a different format might be lexicographically lesser, even if it is actually greater. It is therefore recommended to use assertions to check the format and value of the dates before comparing them.

Existence

The operator Exists checks that the element is present in the response.

The operator Does not exist is the exact opposite of Exists.

Length

The length operators are:

  • Equals
  • Greater than or equal
  • Less than or equal

The length is computed as follows:

  • On strings, it returns the number of characters in the string.
  • On JSON or XML arrays, it returns the number of elements in the array.
  • On JSON objects, it returns the number of keys in the object.
  • On null, number, and booleans, N/A - an error will be displayed.
  • On multi-valued headers, it returns the number of values for the header.
Information noteNote: A header can have multiple values if it is present multiple times or if it contains a comma-separated list of values.

Content

The content operators are:

  • Contains
  • Does not contain

They work this way:

  • On strings, it checks that the element contains the given substring.

    Darth Vader contains arth

  • On JSON arrays, it checks that the element contains all of the given values. The expected values can be either a JSON array or comma-separated values.

    [ "Darth Vader", "Luke Skywalker", "Leia Skywalker" ] contains "Darth Vader", "Luke Skywalker"

    [ "Darth Vader", "Luke Skywalker", "Leia Skywalker" ] contains [ "Darth Vader" ]

  • On JSON objects, it checks that the element contains all of the given keys.

    { "age": 41, "lightSaberColor": "red", "forcePower": "3000" } contains "age","lightSaberColor"

    { "age": 41, "lightSaberColor": "red", "forcePower": "3000" } contains [ "forcePower" ]

  • On arrays of XML nodes, it checks that the element contains all of the given nodes listed as an array of nodes.

    [<title lang="en">Harry Potter</title>, <title lang="en">XQuery Kick Start</title>, <title lang="en">Learning XML</title>] contains [<title lang="en">Harry Potter</title>, <title lang="en">Learning XML</title>]

  • On null, numbers, and booleans: N/A - an error will be displayed

Regular expression matching

The operator Matches allows you to verify that an element matches a given regular expression.

The operator will match the element against valid JavaScript regular expressions. They can be of two forms:

  • Without flags: ^Darth matches Darth Vader.
  • With flags: /^darth/i matches Darth Vader.

A more complete documentation can be found on MDN.

If you want to learn how to craft a regular expression and test it against real-life examples, we recommend the Regex101 website. Just make sure you have selected JavaScript flavor in the left menu for your regular expressions.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!