Basic Example

Our test is going to be a contract with a unit (empty) storage. Its (only) entry point will take a parameter of type unit and inside that entry point will be our test. Entry points must return a list of operation and the new value of the storage. It is not relevant for the test: it will just run and perform tests. Let's have a nothing helper which is the pair of the empty list of operations and unit.

So the simplest test tests/empty.liq we can write is:

type storage = unit

let nothing : operation list * unit = [], ()

let%entry test (_param : unit) (_storage : unit) =
    nothing

Which we can compile, and run with the test.sh script from the previous section.

$ ./../test.sh ../tests/empty.liq
Compiling ../tests/empty.liq...

Module Techel
Contract Multi
Main contract Empty
File "../tests/empty.liq.techel" generated
If tezos is compiled, you may want to typecheck with:
  tezos-client typecheck script ../tests/empty.liq.techel

Running test ../tests/empty.liq.techel

Running test `Empty`

running test script...
   timestamp: 1970-01-01 00:00:00 +00:00

Done running test `Empty`

That was boring. Let's write something slightly more interesting. This new test tests/basic.liq will

  • create an account with 13 tez,
  • check that its balance after deployment is 13 tez,
  • make a transfer of 29 tez,
  • check that the new balance is 42 tez.
type storage = unit

let nothing : operation list * unit = [], ()

let%entry test (_param : unit) (_storage : unit) =
    let delegate : key_hash option = None in
    let operation, address =
        Account.create
            ~manager:tz1YLtLqD1fWHthSVHPD116oYvsd4PTAHUoc
            ~delegate
            ~delegatable:true
            ~amount:13tz
    in
    (* Apply the operation so that we can interact with the account. *)
    Techel.apply_operations [operation];

    (* Contract is now live. *)
    let balance = Techel.get_balance address in
    if balance <> 13tz then (
        failwith "balance should be 13tz"
    );

    let account_contract =
        match UnitContract.at address with
        | None -> failwith "could not retrieve account"
        | Some c -> c
    in

    let operation = Contract.call ~dest:account_contract ~amount:29tz ~parameter:() in
    Techel.apply_operations [operation];

    let balance = Techel.get_balance address in
    if balance <> 42tz then (
        failwith "balance should be 42tz"
    );

    nothing

and run it

$ ./../test.sh ../tests/basic.liq
Compiling ../tests/basic.liq...

Module Techel
Contract Multi
Main contract Basic
File "../tests/basic.liq.techel" generated
If tezos is compiled, you may want to typecheck with:
  tezos-client typecheck script ../tests/basic.liq.techel

Running test ../tests/basic.liq.techel

Running test `Basic`

running test script...
   timestamp: 1970-01-01 00:00:00 +00:00

applying operation CREATE[uid:0] (@address[1], "tz1YLtLqD1fWHthSVHPD116oYvsd4PTAHUoc", None, true, true, 13000000utz) 
                       {
                           storage unit ;
                           parameter unit ;
                           code ...;
                       }
   timestamp: 1970-01-01 00:00:00 +00:00
   live contracts: none
=> live contracts: <anonymous> (13000000utz) address[1]

running test script...
   timestamp: 1970-01-01 00:00:00 +00:00

applying operation TRANSFER[uid:1] address[0]@Basic -> address[1] 29000000utz Unit
   timestamp: 1970-01-01 00:00:00 +00:00
   live contracts: <anonymous> (13000000utz) address[1]

running TRANSFER[uid:1] address[0]@Basic -> address[1] 29000000utz Unit
   timestamp: 1970-01-01 00:00:00 +00:00
=> live contracts: <anonymous> (42000000utz) address[1]

running test script...
   timestamp: 1970-01-01 00:00:00 +00:00

Done running test `Basic`

Let's make sure we are actually testing something. Let's change the final check of tests/basic.liq to

    let balance = Techel.get_balance address in
    if balance <> 12tz then (
        failwith "balance should be 12tz"
    );

in tests/basic_err.liq. The test fails indeed:

$ ./../test.sh ../tests/basic_err.liq
Compiling ../tests/basic_err.liq...

Module Techel
Contract Multi
Main contract Basic_err
File "../tests/basic_err.liq.techel" generated
If tezos is compiled, you may want to typecheck with:
  tezos-client typecheck script ../tests/basic_err.liq.techel

Running test ../tests/basic_err.liq.techel

Running test `Basic_err`

running test script...
   timestamp: 1970-01-01 00:00:00 +00:00

applying operation CREATE[uid:0] (@address[1], "tz1YLtLqD1fWHthSVHPD116oYvsd4PTAHUoc", None, true, true, 13000000utz) 
                       {
                           storage unit ;
                           parameter unit ;
                           code ...;
                       }
   timestamp: 1970-01-01 00:00:00 +00:00
   live contracts: none
=> live contracts: <anonymous> (13000000utz) address[1]

running test script...
   timestamp: 1970-01-01 00:00:00 +00:00

applying operation TRANSFER[uid:1] address[0]@Basic_err -> address[1] 29000000utz Unit
   timestamp: 1970-01-01 00:00:00 +00:00
   live contracts: <anonymous> (13000000utz) address[1]

running TRANSFER[uid:1] address[0]@Basic_err -> address[1] 29000000utz Unit
   timestamp: 1970-01-01 00:00:00 +00:00
=> live contracts: <anonymous> (42000000utz) address[1]

running test script...
   timestamp: 1970-01-01 00:00:00 +00:00

Test `Basic_err` failed:
    Tezos protocol error
        Failure on value "balance should be 12tz" : string

Error
    1 of the 1 testcase failed