Assertion scripts are code snippets that verify if conditions or assumptions in software are true, commonly used in testing to automate checks for expected behavior. They raise errors if assertions fail, ensuring software correctness.Examples#
Assert whether the result returned by the request is correct:Write the returned data to environment variables:Check whether the response body includes a given string.Syntax reference#
Check whether the response body is a given string#
Check the json output value#
Check whether the request response time is less than 200 milliseconds#
Check whether the HTTP status code is 200#
Check whether the HTTP status code name contains a string#
Check whether the POST request status code is correct.#
How to Use the Assertion Libraries#
Apidog has built-in ChaiJS as an assertion library. Below is an example of commonly used assertion test scripts. View the documentation, ChaiJS expect BDD library, for more examples.Assert that the target string contains another string#
Assert that the target is strictly equal to (===) a value#
If the deep flag is on, the assert target is the value.When you turn on the deep flag to use equal and property assertions, the flag will allow subsequent assertions to compare key-value pairs of objects recursively instead of comparing the objects themselves.Assert that the depth is equal to a value, equivalent to deep.equal(value)#
Assert the current environment#
Assert a data structure#
1.
We recommend to use the .a method to check the data type of the template before making other assertions.
2.
Data types are case-sensitive.
Check if array is empty#
Check if an array is empty after using .a method#
Assert the key value of the target object#
Assert whether the target object contains the specified attribute#
1.
The target object must be an object, set, array, or map.
2.
If .keys is not preceded by .all or .any, it defaults to .all.
3.
Since only some data types of the target object can be used with the .keys method, we recommend asserting the data type with the .a method first.
Assert the length of the target object#
Assert the members of the target object (members)#
1.
By default, .members uses a strict comparison.
2.
The order of the members does not affect the result.
Asserts that the target object contains the specified item#
We recommend using .a
method to determine the data type before using .include
. Modified atΒ 2024-07-19 03:00:34