Skip to main content
Version: Current

Testing Your Contracts

Good test coverage is vital for assuring code quality.

tip

Leverage Flow emulator to automate test runs. Test on Flow testnet. Include unit tests; to exercise each feature, and integration tests; to exercise the behavior of different parts of the project as a whole.

Human-driven tests, in which individual testers work manually through user stories via the project's user interface or custom transactions, can also be performed locally. Tests with groups of testers can be performed similarly on testnet in a similar manner.

Finally, unstructured closed testing with a limited audience on testnet can gain valuable information about performance and security by capturing usage data from more organic interaction with the project's smart contracts over days or weeks.

Testing Requirements

It is suggested to follow the following best practices:

  • Every publicly exposed feature of a contract and its resources should have unit tests that check both for success with correct input and for failure with incorrect input. These tests should be capable of being run locally with the Flow emulator, with no or minimal extra resources or configuration, and with a single command.
  • Each user story or workflow that uses the smart contracts should have an integration test that ensures that the series of steps required to complete it does so successfully with test data.

Make sure you test all contracts - and the integration into your application extensively before proceeding to the mainnet. You should aim to replicate all conditions as closely as possible to the usage patterns on mainnet.

Writing Tests

There are official SDKs for Flow in both Go and JavaScript.

In both cases, the code will need to deploy the contracts, configure accounts to interact with them and send transactions to them. It will then have to wait for the transactions to be sealed and check the results by catching exceptions, checking for events, and querying state using scripts.

Go Tests

Tests in Go can be written using flow-go-sdk and the go test command.

You can find examples of Go tests in the following projects: flow-core-contracts, flow-nft, flow-ft.

info

These tests are tied to the emulator but can be refactored to run on testnet

JavaScript Tests

Tests in JavaScript can be written using flow-js-testing.

It is critical to test your applications and contracts thoroughly on the testnet as part of your road to the mainnet. Testing will help you understand how to create stable and robust applications using the Flow development stack.

Testing Your Application

Automated Testing of Contract Code

All contracts should include test coverage for all contract functions. Make sure you've accounted for success and failure cases appropriately.

Tests should also be runnable in automated environments (CI). You can use the JavaScript testing framework to create tests for your smart contract code.

Stress Testing Live Applications Before Mainnet

Once you deployed your application to the testnet, you should record how your application handles non-trivial amounts of traffic to ensure there are no issues.

tip

Get familiar with the Cadence anti-patterns to avoid avoid problematic or unintended behavior.