Cadence Testing Framework
The Cadence testing framework provides a convenient way to write tests for Cadence programs in Cadence.
This functionality is provided by the built-in Test
contract.
info
The testing framework can only be used off-chain, e.g. by using the Flow CLI.
Tests must be written in the form of a Cadence script.
A test script may contain testing functions that starts with the test
prefix,
a setup
function that always runs before the tests,
a tearDown
function that always runs at the end of all test cases,
a beforeEach
function that runs before each test case,
and an afterEach
function that runs after each test case.
All the above four functions are optional.
_34// A `setup` function that always runs before the rest of the test cases._34// Can be used to initialize things that would be used across the test cases._34// e.g: initialling a blockchain backend, initializing a contract, etc._34access(all) fun setup() {_34}_34_34// The `beforeEach` function runs before each test case. Can be used to perform_34// some state cleanup before each test case, among other things._34access(all) fun beforeEach() {_34}_34_34// The `afterEach` function runs after each test case. Can be used to perform_34// some state cleanup after each test case, among other things._34access(all) fun afterEach() {_34}_34_34// Valid test functions start with the 'test' prefix._34access(all) fun testSomething() {_34}_34_34access(all) fun testAnotherThing() {_34}_34_34access(all) fun testMoreThings() {_34}_34_34// Test functions cannot have any arguments or return values._34access(all) fun testInvalidSignature(message: String): Bool {_34}_34_34// A `tearDown` function that always runs at the end of all test cases._34// e.g: Can be used to stop the blockchain back-end used for tests, etc. or any cleanup._34access(all) fun tearDown() {_34}
Test Standard Library​
The testing framework can be used by importing the built-in Test
contract:
_10import Test