Skip to main content
Version: Current

Run Cadence tests with the Flow CLI

The Flow CLI provides a command to run Cadence tests.


_10
flow test /path/to/test_script.cdc

⚠️ The test command expects configuration to be initialized. See flow init command.

Example Usage

A simple Cadence script test_script.cdc, which has a test case for running a cadence script on-chain:


_12
import Test
_12
_12
pub fun testSimpleScript() {
_12
var blockchain = Test.newEmulatorBlockchain()
_12
var result = blockchain.executeScript(
_12
"pub fun main(a: Int, b: Int): Int { return a + b }",
_12
[2, 3]
_12
)
_12
_12
assert(result.status == Test.ResultStatus.succeeded)
_12
assert((result.returnValue! as! Int) == 5)
_12
}

Above test-script can be run with the CLI as follows, and the test results will be printed on the console.


_10
> flow test test_script.cdc
_10
_10
Running tests...
_10
_10
Test results: "test_script.cdc"
_10
- PASS: testSimpleScript

To learn more about writing tests in Cadence, have a look at the Cadence testing framework.

Flags

Coverage

  • Flag: --cover
  • Default: false

Use the cover flag to calculate coverage report for the code being tested.


_10
> flow test --cover test_script.cdc
_10
_10
Running tests...
_10
_10
Test results: "test_script.cdc"
_10
- PASS: testSimpleScript
_10
Coverage: 100.0% of statements

Coverage Report File

  • Flag: --coverprofile
  • Valid inputs: valid filename
  • Default: coverage.json

Use the coverprofile to specify the filename where the calculated coverage report is to be written.