Skip to content

Commit

Permalink
Rework testing topics (#266)
Browse files Browse the repository at this point in the history
* move testing topic to developing section

* Recast this topic to testing locally

* headings

* teztnets.com

* Ways to work with periodic testnets

* Example of using octez-client mockup mode

* Correct link

* Headings

* Docker tips

* some tweaks

* simplify

* Table of networks and characteristics

* Clarifications
  • Loading branch information
timothymcmackin authored Feb 6, 2024
1 parent 1749362 commit c8d2a8b
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 63 deletions.
2 changes: 1 addition & 1 deletion docs/developing/dev-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ To test smart contracts and dApps, you can use these test environments:
- Test networks behave like Tezos Mainnet but have differences that make it easier to test on them, such as faucets that provide free tokens and reduced block times for faster testing.
- Sandbox environments like [Flextesa](https://tezos.gitlab.io/flextesa/) run Tezos nodes locally on your computer in a sandbox mode.

For more information about test environments, see [Using sandboxes and testnets](./testnets).
For more information about test environments, see [Testing on sandboxes and testnets](./testnets).
2 changes: 1 addition & 1 deletion docs/developing/octez-client/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ octez-client show address local_account

The account address (technically the hash of the public key) starts with `tz1`, `tz2`, or `tz3`.
You can use this address to send tez to this account, such as from a faucet if you are using a testnet.
See [Using sandboxes and testnets](../testnets).
See [Testing on sandboxes and testnets](../testnets).

<!-- TODO
## Importing pregenerated accounts
Expand Down
43 changes: 32 additions & 11 deletions docs/smart-contracts/testing.md → docs/developing/testing.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
---
title: Testing smart contracts
title: Testing locally
authors: 'Yuxin Li'
last_update:
date: 6 November 2023
date: 23 January 2024
---

## Introduction
Tezos blockchain smart contracts are immutable after deployment, so you must rigorously test to ensure functionality, prevent errors, and avoid potential financial losses. Importantly, contract testing doesn't require any tokens or a wallet account to execute.
Tezos smart contracts are immutable after deployment, so you must rigorously test them before deploying them to ensure functionality, prevent errors, and avoid potential financial losses. Importantly, contract testing doesn't require any tokens or a wallet account to execute.

High-level languages come with tools to help write tests, and some testing tools can be used independently of the language used to write the smart contract.
## Tools for local testing

- The Michelson interpreter is an OCaml function that can be used by tools to simulate a call to any entry point of any smart contract, given an initial value of the storage and parameters. Some programming languages like LIGO or SmartPy use this as part of their testing frameworks.

- The mockup mode of `octez-client` can be used to test contract calls and other features such as some RPC calls, all without running an actual node, saving the time of going through the consensus mechanism and waiting to get blocks created and validated. Tools like Completium, built by the team behind the Archetype language, use this for their testing framework. Find out more in the [documentation of the mockup mode](https://tezos.gitlab.io/user/mockup.html).

For example, when you compile the contract in the tutorial [Create a smart contract](../tutorials/smart-contract) to Michelson, its first line defines the parameter type that the contract accepts:

```
parameter (or (unit %reset) (or (int %decrement) (int %increment)))
```

You can call this contract in mockup mode by passing the compiled contract file, the storage value as a Michelson expression, and the parameter value to pass as a Michelson expression.
For example, this command sets the storage to 4 and passes 5 to the `Increment` entrypoint:

```bash
octez-client --mode mockup run script Counter.tz on storage 4 and input "(Right (Right 5))"
```

The response in the console shows the new value of the storage and any operations emitted.

## Testing in high-level languages

High-level languages come with tools to help write tests locally, and some testing tools can be used independently of the language used to write the smart contract.
For example, [SmartPy](https://smartpy.io/manual/scenarios/overview) includes syntax dedicated to testing.

The following SmartPy test code snippet is for a Tezos smart contract that acts like a calculator. The code defines a series of tests to check the functionality of the calculator contract.
Expand All @@ -34,11 +56,7 @@ if "templates" not in __name__:
```
The test scenario runs these operations sequentially and would check if all operations execute as expected and if the final result matches the expected value.
## Prerequisites
- Set up an wallet account on Tezos with some tez to pay the fees
- Ensure that you have obtained the [Tezos client](../developing/octez-client/installing)
# Structure of a test scenario
## Structure of a test scenario
A test scenario usually consists of the following steps:
Expand Down Expand Up @@ -88,4 +106,7 @@ For more information about avoiding flaws in contracts, see [Avoiding flaws](htt
- SmartPy: [Tests and scenarios](https://smartpy.io/manual/scenarios/overview)
- LIGO: [Testing LIGO](https://ligolang.org/docs/advanced/testing)
Upon test successful, you can deploy your smart contract!
## Next steps
When you're done testing contracts locally, you can deploy them to a test network and test them there.
See [Testing on sandboxes and testnets](./testnets).
Loading

0 comments on commit c8d2a8b

Please sign in to comment.