Contract testing
Last updated
Last updated
Contract testing is a testing approach used in software development that involves testing the interactions between two different services or systems by defining and verifying the contracts or agreements between them.
In a typical scenario, when two services communicate with each other through an API, the contract testing approach involves defining the expected input and output data for each API endpoint, and then writing tests to verify that the API endpoint behaves as expected.
Contract testing helps in detecting and preventing issues that may arise due to changes made to the API, without having to run end-to-end tests. This allows for faster and more efficient testing of software systems, reducing the time and effort required for testing.
Overall, contract testing helps improve the reliability, stability, and resilience of software systems by ensuring that the different components of the system work together as intended, and are compatible with each other.
Contract testing with Loadmill is easy. To create contract validation of an API call, use JSON schema or JSON contains as following:
Enforcing JSON Schema of an API: With 'JSON Schema' you can explicitly enforce multiple of aspects of your json data. In the request editor assertions insert new entry, use '__responseBody' built-in parameter and ensure the values are of the type "JSON Schema", then insert a valid json-schema.
In the following table you can see an example of the schema and a typical response that comply with the schema.
Schema | Response |
---|---|
Enforcing JSON structure using JSON-Contains:
'Json-contains' is a lightweight capability in Loadmill to enforce JSON data.
In the request editor assertions insert new entry, use '__responseBody' built-in parameter and ensure the values are of the type "JSON Contains", then insert an expected json or a subset that can contain less fields.
Note, to ensure existence of a field ignoring it's value use "*"
notation.
In the following table you can see an example of the expected JSON-Contains object and a typical response that comply with it.
JSON-Contains object | Response |
---|---|
Automatic assertions Loadmill can generate 'JSON schema' or 'JSON contains' assertions automatically during test composition. Such assertions can serve are a quick baseline that can be adjusted later on. To turn automatic JSON validations.
Locate the 'JSON Validation section in the list', make sure the toggle turned on and adjust the type according to the needs.
From now on, every generated test with loadmill composers will cary json validation assertions.
head to settings icon -> recordings:
if 'JSON Validation' section doesn't exist, try adding it by clicking on at the top right corner and selecting it from the list.
(optional) Click on the '+' to add ignored keys.