Contract testing

Intro

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

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.

  • 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.

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.

  1. Locate the 'JSON Validation section in the list', make sure the toggle turned on and adjust the type according to the needs.

  1. From now on, every generated test with loadmill composers will cary json validation assertions.

Last updated