Skip to main content
Version: Next

Run Cadence tests

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:


_15
import Test
_15
_15
pub let blockchain = Test.newEmulatorBlockchain()
_15
_15
pub fun testSumOfTwo() {
_15
let scriptResult = blockchain.executeScript(
_15
"pub fun main(a: Int, b: Int): Int { return a + b }",
_15
[2, 3]
_15
)
_15
_15
Test.expect(scriptResult, Test.beSucceeded())
_15
_15
let sum = scriptResult.returnValue! as! Int
_15
Test.assertEqual(5, sum)
_15
}

The 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
Test results: "test_script.cdc"
_10
- PASS: testSumOfTwo

To learn more about writing tests in Cadence, take 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
Test results: "test_script.cdc"
_10
- PASS: testSumOfTwo
_10
Coverage: 96.5% of statements

Coverage Report File

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

Use the coverprofile to specify the filename where the calculated coverage report is to be written. Supported filename extensions are .json and .lcov.


_10
$ flow test --cover test_script.cdc
_10
_10
$ cat coverage.json
_10
_10
$ flow test --cover --coverprofile="coverage.lcov" test_script.cdc
_10
_10
$ cat coverage.lcov

Coverage Code Type

  • Flag: --covercode
  • Valid inputs: "all", "contracts"
  • Default: "all"

Use the covercode flag to calculate coverage report only for certain types of code. A value of "contracts" will exclude scripts and transactions from the coverage report.


_10
$ flow test --cover --covercode="contracts" test_script.cdc
_10
_10
Test results: "tests/test_script.cdc"
_10
- PASS: testSumOfTwo
_10
There are no statements to cover

Since we did not use any contracts in our sample test script, there is no coverage percentage to be reported.