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
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 sandboxed node).
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
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
mockup protocols. In this example, we will use Edo for testing, so
the command we use for creating a mockup instance on the directory
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:
--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
Append ("bar") on our contract with storage
the contract updates its storage to
As a first sanity check, we can confirm that the storage is currently
Then, we execute a call to our contract with parameter
("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!