How to Write Differential Tests
To experiment with writing a differential test, open a contract that you’ve deployed, or checkout an example from the [soroban-examples] repository and deploy it. Assuming the contract has been deployed, and changes are being made to the local copy. We need to check that unchanged behavior in the contract hasn’t changed compared to what is deployed.-
Use the stellar contract fetch command to fetch the contract that’s already deployed. The contract already deployed will be used as a baseline that the local copy is expected to behave like.
-
Import the contract into the tests with the
contractimport!macro. -
Write a test that runs the same logic for the deployed contract and the local contract, comparing the result. Assuming the increment example is in use, the test would look something like the following.
- Run the test to compare the baseline and local observable outcomes.
- Create an environment, the
Env. - Import the Wasm contract to compare with.
- Register the local contract to be tested.
- Invoke functions using a client.
- Assert equality.