Process
Unit Testing
Last updated:
December 13, 2021

Unit tests are tests that test smart contracts in isolation. They are analogous to unit tests in traditional software programming. All smart contract development frameworks support unit testing natively with fairly similar functionality.

Limit repetition

Use text fixtures, deployment scripts as well as test pre-hooks (like beforeAll or before in JavaScript based smart contract frameworks) to prevent code repetition and make tests smaller and more readable.

Be consistent

As generally recommended in testing, we advocate using the same conventions for deploying, transacting smart contracts and reading on-chain state consistently.

Mocking

Occasionally the production smart contracts rely on certain external components (e.g., oracles) and cannot be deployed in isolation. In this case, it can make sense to write simple smart contracts called "Mocks" that follow the interface of an external component (e.g., oracle contract) but implement it in a simple way. For example a mock oracle could be programmed to just return a storage variable that can be set manually during testing.

See Also: