Skip to main content
Version: Current

Testing Your Contracts

Testing is an essential part of smart contract development to ensure the correctness and reliability of your code. Cadence Testing Framework provides a convenient way to write tests for your programs, allowing you to verify the functionality and correctness of your smart contracts.

Install Flow CLI

The Flow CLI is the primary tool for developing, testing, and deploying smart contracts to the Flow network.

If you haven't installed the Flow CLI yet and have homebrew installed, simply run brew install flow-cli. Alternatively, refer to the Flow CLI installation instructions.

Create a new project

In your preferred code editor, create a new directory for your project and navigate to it in the terminal. Then initialize a new Flow project by running the command flow init. This will create a flow.json file that contains the project configuration.


_10
mkdir test-cadence
_10
cd test-cadence
_10
flow init

Write a simple smart contract

In your code editor, create a new file called calculator.cdc and add the following code:

calculator.cdc

_10
access(all) contract Calculator {
_10
access(all) fun add(a: Int, b: Int): Int {
_10
return a + b;
_10
}
_10
_10
access(all) fun subtract(a: Int, b: Int): Int {
_10
return a - b;
_10
}
_10
}

Add the smart contract to the config

Run flow config add contract and add the created calculator.cdc contract to the config with the name as Calculator. This command adds your contract name and location under the contracts key in flow.json.


_10
Enter name: Calculate
_10
Enter contract file location: ./calculator.cdc

Write unit test cases

In the same directory, create a new file called calculator_test.cdc and add the following code:

calculator_test.cdc

_12
import Test
_12
import "Calculator" // contract name from the previous step
_12
_12
access(all) let calculator = Calculator()
_12
_12
access(all) fun testAdd() {
_12
Test.assertEqual(5, calculator.add(a: 2, b: 3))
_12
}
_12
_12
access(all) fun testSubtract() {
_12
Test.assertEqual(2, calculator.subtract(a: 5, b: 3))
_12
}

This code

  • imports the Calculator contract from the calculator.cdc file (according to flow.json);
  • creates an calculator instance of the smart-contract;
  • defines two test functions: testAdd() and testSubtract();
  • calls add() and subtract() methods with different input values respectively.

Running the test cases

To run the test cases, use the following command in the terminal:


_10
flow test ./calculator_test.cdc

This command uses the Flow CLI to run the test cases and display the output. You should see the following output:


_10
Test results: "./calculator_test.cdc"
_10
- PASS: testAdd
_10
- PASS: testSubtract

This output indicates that both test cases ran successfully, and the smart contract is functioning as expected.

Advanced Testing Techniques

The Cadence testing framework provides various features and techniques for writing comprehensive test cases. Some of these include:

  • Code Coverage: You can use the --cover flag with the flow test command to view code coverage results when running your tests. This allows you to identify areas of your code that are not adequately covered by your test inputs;
  • Test Fixtures: Test fixtures are reusable components that help you set up the initial state for your test cases. You can create test fixtures in Cadence by defining resource types and using them in your test functions;
  • Assertions: The testing framework provides built-in assertion functions, such as assertEqual, beNil, beEmpty, contain, to help you verify the expected behavior of your smart contracts;
  • Test Suites: You can organize your test cases into test suites to improve the readability and maintainability of your test code. Test suites allow you to group related test cases and set up common test fixtures for all the tests in the suite.
  • Integration tests: You can use Overflow tool to run integration tests against either an local emulator, testnet, mainnet or an in memory instance of the flow-emulator.

By leveraging these advanced testing techniques, you can write more robust and reliable smart contracts in Cadence. In this example, we set up a basic testing environment, wrote a simple smart contract in Cadence, and created a test case to verify its functionality. We then used the Flow CLI to run the test case and confirm that the smart contract is working correctly.

This is a basic example, and there are many more advanced features and techniques you can explore when working with the Cadence Testing Framework.

For more in-depth tutorials and documentation, refer to the official Cadence language documentation and the Flow CLI documentation.

Writing Tests

There are official SDKs/frameworks for Flow in Cadence, Go and JavaScript.

In all three cases, the test 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.

Cadence tests

Cadence comes with built-in support for code coverage, as well as a native testing framework which allows developers to write their tests using Cadence. This framework is bundled with the Flow CLI tool, which includes a dedicated command for running tests (flow test).

You can find examples of Cadence tests in the following projects: hybrid-custody, flow-nft, flow-ft. Visit the documentation to view all the available features.

The Hybrid Custody project is a prime example which utilizes both the Cadence testing framework and code coverage in its CI.

Hybrid Custody CI

There is also a repository which contains some sample contracts and their tests.

Automated CI Coverage Report

Coverage Report Visualization

info

The Cadence testing framework utilizes the emulator under the hood.

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.

References