Compare

The Compare assertion interface is as follows:

A Compare assertion is used for comparing 2 values. In the Expected value type field you should specify the type of values you want to compare (strings, numbers or JSON objects).

A set of available operations that you can select in the Operator field depends on the type you chose. For strings and numbers, you can use the following operators:

  • equal - checks if the values are equal

  • not equal - checks if the values are not equal

  • greater - checks if one value is greater than another one

  • greater or equal - checks if one value is greater than another or equal to it

  • less - checks if one value is less than another one

  • less or equal - checks if one value is less than another or equal to it

It is important to note that the result of comparing two values that can be represented both by strings and numbers might seem rather surprising. For example, let's assert that the number 100 is greater than 2. The assertion will be successful. However if you compare the same string values ("100" is greater than "2"), the assertion will fail. That's because strings are compared symbol-by-symbol in alphabetical order.

The following operators are available for comparing objects:

  • equal - checks if two object structures are equal

  • not equal - checks if the object structures are different

  • is subset of - checks if one structure is a subset of another structure

  • is superset of - checks if one structure is a superset of another structure

Examples of the is subset of and is superset of operators

Let's say a current value is:

[
    {
        "name": "Mike"
    }
]

And an expected value is:

[
    { 
        "id": 0,
        "name": "John",
        "name": 24
    },
    {
        "id": 1,
        "name": "Mike",
        "age": 28
    }
]

In this example the current value is a subset of the expected value. Conversely, the expected value is a superset of the current value.

The structures are compared recursively, every nesting level is checked. In the above example the fact that at the top level both values are arrays is verified first, then each element of the current array is compared to each element of the expected one until the first match is encountered. The objects are compared by matching string keys and their values.

Note that while comparing array elements the algorithm is looking for a subsequence rather than a single element. For example, the [2,4] array is a subset of the [1,2,3,4], but the [4, 2] array is not.

Error Fixing

The error fixing algorithm depends on the comparator type.

  • equal - the current value is assigned to the expected one

  • not equal - the comparator type changes to equal

  • greater - the comparator type changes to greater or equal and the current value is assigned to the expected one

  • greater or equal - the current value is assigned to the expected one

  • less - the comparator type changes to less or equal and the current value is assigned to the expected one

  • less or equal - the current value is assigned to the expected one

File Representation

The Assertion is of the compare type in the file. The type description can be found in the Assertion file representation documentation in the #/definitions/CompareAssertion definition.

Last updated