Testing Michelson code
Testing Michelson code
There are multiple frameworks for testing Michelson contracts, we will not get into details, but here is a list of tutorials showing how to test contracts in Michelson:
Another alternative is to use Tezos's binary tezos-client
directly. There's a new
mockup mode which is does
not need a Tezos node to be running (albeit this is less similar to
mainnet than running a Tezos sandbox node).
Testing with tezos-client's mockup
We show the main steps that need to be done to use the mockup mode to test our LIGO contracts. As a first step, we need to compile our LIGO contract to Michelson code. Suppose we write the following simple contract:
To obtain Michelson code from it, we run the LIGO compiler like so:
Instead of outputting the resulted compiled code in the screen, we can
tell LIGO to write it in a file called mockup_testme.tz:
Now it is time to test this Michelson code we obtained: we want to execute it using the mockup mode.
Before anything, make sure you have installed tezos-client, a simple
way to do so is by using opam (opam install tezos-client).
We can list all the protocols available using tezos-client list
mockup protocols. In this example, we will use Edo for testing, so
the command we use for creating a mockup instance on the directory
/tmp/mockup/ is:
This command returns a list of Tezos addresses that we can use with the client in subsequent commands. As recommended in the Tezos documentation, we can add a shell alias to avoid mistakes:
We can list the addresses returned above by running:
We are now ready to originate (or "deploy") the contract on our mockup Tezos:
The --init argument ("foo") is the initial storage for our
deployed contract. In case we had a more complex storage, we could
have used LIGO's compile-storage sub-command to compile a LIGO
expression to a Michelson storage.
Now it is time to test! The property we want to check is that if we
execute Append ("bar") on our contract with storage "foo", then
the contract updates its storage to "foobar".
As a first sanity check, we can confirm that the storage is currently "foo":
Then, we execute a call to our contract with parameter Append
("bar"). To do so, we first compile the parameter as follows:
So our parameter is simply the string (notice that the constructor
Append was removed). We execute a call to the contract with this
compiled parameter as follows:
We have chosen bootstrap2 as the origin of this call (for no
particular reason, any address could do).
We can finally check that that our property holds: the storage is now "foobar":
Good! Our contract passed the test successfully!