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.

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 will always run before the tests, and a tearDown function that will always run at the end of all test cases. Both setup and tearDown functions are optional.

1
// A `setup` function that will always run before the rest of the methods.
2
// Can be used to initialize things that would be used across the test cases.
3
// e.g: initialling a blockchain backend, initializing a contract, etc.
4
pub fun setup() {
5
}
6
7
// Test functions start with the 'test' prefix.
8
pub fun testSomething() {
9
}
10
11
pub fun testAnotherThing() {
12
}
13
14
pub fun testMoreThings() {
15
}
16
17
// A `tearDown` function that will always run at the end of all test cases.
18
// e.g: Can be used to stop the blockchain back-end used for tests, etc. or any cleanup.
19
pub fun tearDown() {
20
}

Test Standard Library

The testing framework can be used by importing the built-in Test contract:

1
import Test

Assertion

assert

1
fun assert(_ condition: Bool, message: String)

Fails a test-case if the given condition is false, and reports a message which explains how the condition is false.

The message argument is optional.

fail

1
fun fail(message: String)

Immediately fails a test-case, with a message explaining the reason to fail the test.

The message argument is optional.

expect

The expect function tests a value against a matcher (see matchers section), and fails the test if it's not a match.

1
fun expect(_ value: AnyStruct, _ matcher: Matcher)

Matchers

A matcher is an object that consists of a test function and associated utility functionality.

1
pub struct Matcher {
2
3
pub let test: ((AnyStruct): Bool)
4
5
pub init(test: ((AnyStruct): Bool)) {
6
self.test = test
7
}
8
9
/// Combine this matcher with the given matcher.
10
/// Returns a new matcher that succeeds if this and the given matcher succeed.
11
///
12
pub fun and(_ other: Matcher): Matcher {
13
return Matcher(test: fun (value: AnyStruct): Bool {
14
return self.test(value) && other.test(value)
15
})
16
}
17
18
/// Combine this matcher with the given matcher.
19
/// Returns a new matcher that succeeds if this or the given matcher succeeds.
20
///
21
pub fun or(_ other: Matcher): Matcher {
22
return Matcher(test: fun (value: AnyStruct): Bool {
23
return self.test(value) || other.test(value)
24
})
25
}
26
}

The test function defines the evaluation criteria for a value, and returns a boolean indicating whether the value conforms to the test criteria defined in the function.

The and and or functions can be used to combine this matcher with another matcher to produce a new matcher with multiple testing criteria. The and method returns a new matcher that succeeds if both this and the given matcher are succeeded. The or method returns a new matcher that succeeds if at-least this or the given matcher is succeeded.

A matcher that accepts a generic-typed test function can be constructed using the newMatcher function.

1
fun newMatcher<T: AnyStruct>(_ test: ((T): Bool)): Test.Matcher

The type parameter T is bound to AnyStruct type. It is also optional.

For example, a matcher that checks whether a given integer value is negative can be defined as follows:

1
let isNegative = Test.newMatcher(fun (_ value: Int): Bool {
2
return value < 0
3
})
4
5
// Use `expect` function to test a value against the matcher.
6
Test.expect(-15, isNegative)

Built-in matcher functions

The Test contract provides some built-in matcher functions for convenience.

  • fun equal(_ value: AnyStruct): Matcher

    Returns a matcher that succeeds if the tested value is equal to the given value. Accepts an AnyStruct value.

Blockchain

A blockchain is an environment to which transactions can be submitted to, and against which scripts can be run. It imitates the behavior of a real network, for testing.

1
/// Blockchain emulates a real network.
2
///
3
pub struct Blockchain {
4
5
pub let backend: AnyStruct{BlockchainBackend}
6
7
init(backend: AnyStruct{BlockchainBackend}) {
8
self.backend = backend
9
}
10
11
/// Executes a script and returns the script return value and the status.
12
/// `returnValue` field of the result will be `nil` if the script failed.
13
///
14
pub fun executeScript(_ script: String, _ arguments: [AnyStruct]): ScriptResult {
15
return self.backend.executeScript(script, arguments)
16
}
17
18
/// Creates a signer account by submitting an account creation transaction.
19
/// The transaction is paid by the service account.
20
/// The returned account can be used to sign and authorize transactions.
21
///
22
pub fun createAccount(): Account {
23
return self.backend.createAccount()
24
}
25
26
/// Add a transaction to the current block.
27
///
28
pub fun addTransaction(_ tx: Transaction) {
29
self.backend.addTransaction(tx)
30
}
31
32
/// Executes the next transaction in the block, if any.
33
/// Returns the result of the transaction, or nil if no transaction was scheduled.
34
///
35
pub fun executeNextTransaction(): TransactionResult? {
36
return self.backend.executeNextTransaction()
37
}
38
39
/// Commit the current block.
40
/// Committing will fail if there are un-executed transactions in the block.
41
///
42
pub fun commitBlock() {
43
self.backend.commitBlock()
44
}
45
46
/// Executes a given transaction and commit the current block.
47
///
48
pub fun executeTransaction(_ tx: Transaction): TransactionResult {
49
self.addTransaction(tx)
50
let txResult = self.executeNextTransaction()!
51
self.commitBlock()
52
return txResult
53
}
54
55
/// Executes a given set of transactions and commit the current block.
56
///
57
pub fun executeTransactions(_ transactions: [Transaction]): [TransactionResult] {
58
for tx in transactions {
59
self.addTransaction(tx)
60
}
61
62
let results: [TransactionResult] = []
63
for tx in transactions {
64
let txResult = self.executeNextTransaction()!
65
results.append(txResult)
66
}
67
68
self.commitBlock()
69
return results
70
}
71
72
/// Deploys a given contract, and initilizes it with the arguments.
73
///
74
pub fun deployContract(
75
name: String,
76
code: String,
77
account: Account,
78
arguments: [AnyStruct]
79
): Error? {
80
return self.backend.deployContract(
81
name: name,
82
code: code,
83
account: account,
84
arguments: arguments
85
)
86
}
87
}

The BlockchainBackend provides the actual functionality of the blockchain.

1
/// BlockchainBackend is the interface to be implemented by the backend providers.
2
///
3
pub struct interface BlockchainBackend {
4
5
pub fun executeScript(_ script: String, _ arguments: [AnyStruct]): ScriptResult
6
7
pub fun createAccount(): Account
8
9
pub fun addTransaction(_ tx: Transaction)
10
11
pub fun executeNextTransaction(): TransactionResult?
12
13
pub fun commitBlock()
14
15
pub fun deployContract(
16
name: String,
17
code: String,
18
account: Account,
19
arguments: [AnyStruct]
20
): Error?
21
}

Creating a blockchain

A new blockchain instance can be created using the newEmulatorBlockchain method. It returns a Blockchain which is backed by a new Flow Emulator instance.

1
let blockchain = Test.newEmulatorBlockchain()

Creating accounts

It may be necessary to create accounts during tests for various reasons, such as for deploying contracts, signing transactions, etc. An account can be created using the createAccount function.

1
let acct = blockchain.createAccount()

The returned account consist of the address of the account, and a publicKey associated with it.

1
/// Account represents info about the account created on the blockchain.
2
///
3
pub struct Account {
4
pub let address: Address
5
pub let publicKey: PublicKey
6
7
init(address: Address, publicKey: PublicKey) {
8
self.address = address
9
self.publicKey = publicKey
10
}
11
}

Executing scripts

Scripts can be run with the executeScript function, which returns a ScriptResult. The function takes script-code as the first argument, and the script-arguments as an array as the second argument.

1
let result = blockchain.executeScript("pub fun main(a: String) {}", ["hello"])

The script result consists of the status of the script execution, and a returnValue if the script execution was successful, or an error otherwise (see errors section for more details on errors).

1
/// The result of a script execution.
2
///
3
pub struct ScriptResult {
4
pub let status: ResultStatus
5
pub let returnValue: AnyStruct?
6
pub let error: Error?
7
8
init(status: ResultStatus, returnValue: AnyStruct?, error: Error?) {
9
self.status = status
10
self.returnValue = returnValue
11
self.error = error
12
}
13
}

Executing transactions

A transaction must be created with the transaction code, a list of authorizes, a list of signers that would sign the transaction, and the transaction arguments.

1
/// Transaction that can be submitted and executed on the blockchain.
2
///
3
pub struct Transaction {
4
pub let code: String
5
pub let authorizers: [Address]
6
pub let signers: [Account]
7
pub let arguments: [AnyStruct]
8
9
init(code: String, authorizers: [Address], signers: [Account], arguments: [AnyStruct]) {
10
self.code = code
11
self.authorizers = authorizers
12
self.signers = signers
13
self.arguments = arguments
14
}
15
}

The number of authorizers must match the number of AuthAccount arguments in the prepare block of the transaction.

1
let tx = Test.Transaction(
2
code: "transaction { prepare(acct: AuthAccount) {} execute{} }",
3
authorizers: [account.address],
4
signers: [account],
5
arguments: [],
6
)

There are two ways to execute the created transaction.

  • Executing the transaction immediately

    1
    let result = blockchain.executeTransaction(tx)

    This may fail if the current block contains transactions that have not being executed yet.

  • Adding the transaction to the current block, and executing it later.

    1
    // Add to the current block
    2
    blockchain.addTransaction(tx)
    3
    4
    // Execute the next transaction in the block
    5
    let result = blockchain.executeNextTransaction()

The result of a transaction consists of the status of the execution, and an Error if the transaction failed.

1
/// The result of a transaction execution.
2
///
3
pub struct TransactionResult {
4
pub let status: ResultStatus
5
pub let error: Error?
6
7
init(status: ResultStatus, error: Error) {
8
self.status = status
9
self.error = error
10
}
11
}

Commit block

commitBlock block will commit the current block, and will fail if there are any un-executed transactions in the block.

1
blockchain.commitBlock()

Deploying contracts

A contract can be deployed using the deployContract function of the Blockchain.

1
let contractCode = "pub contract Foo{ pub let msg: String; init(_ msg: String){ self.msg = msg } pub fun sayHello(): String { return self.msg } }"
2
3
let err = blockchain.deployContract(
4
name: "Foo",
5
code: contractCode,
6
account: account,
7
arguments: ["hello from args"],
8
)

An Error is returned if the contract deployment fails. Otherwise, a nil is returned.

Configuring import addresses

A common pattern in Cadence projects is to define the imports as file locations and specify the addresses corresponding to each network in the Flow CLI configuration file. When writing tests for a such project, it may also require to specify the addresses to be used during the tests as well. However, during tests, since accounts are created dynamically and the addresses are also generated dynamically, specifying the addresses statically in a configuration file is not an option.

Hence, the test framework provides a way to specify the addresses using the useConfiguration(_ configuration: Test.Configuration) function in Blockchain.

The Configuration struct consists of a mapping of import locations to their addresses.

1
/// Configuration to be used by the blockchain.
2
/// Can be used to set the address mapping.
3
///
4
pub struct Configuration {
5
pub let addresses: {String: Address}
6
7
init(addresses: {String: Address}) {
8
self.addresses = addresses
9
}
10
}

The Blockchain.useConfiguration is a run-time alternative for statically defining contract addresses in the flow.json config file.

The configurations can be specified during the test setup as a best-practice.

e.g: Assume running a script that imports FooContract and BarContract. The import locations for the two contracts can be specified using the two placeholders "FooContract" and "BarContract". These placeholders can be any unique strings.

1
import FooContract from "FooContract"
2
import BarContract from "BarContract"
3
4
pub fun main() {
5
// do something
6
}

Then, before executing the script, the address mapping can be specified as follows:

1
pub var blockchain = Test.newEmulatorBlockchain()
2
pub var accounts: [Test.Account] = []
3
4
pub fun setup() {
5
// Create accounts in the blockchain.
6
7
let acct1 = blockchain.createAccount()
8
accounts.append(acct1)
9
10
let acct2 = blockchain.createAccount()
11
accounts.append(acct2)
12
13
// Set the configuration with the addresses.
14
// They keys of the mapping should be the placeholders used in the imports.
15
16
blockchain.useConfiguration(Test.Configuration({
17
"FooContract": acct1.address,
18
"BarContract": acct2.address
19
}))
20
}

The subsequent operations on the blockchain (e.g: contract deployment, script/transaction execution) will resolve the import locations to the provided addresses.

Errors

An Error maybe returned when an operation (such as executing a script, executing a transaction, etc.) is failed. Contains a message indicating why the operation failed.

1
// Error is returned if something has gone wrong.
2
//
3
pub struct Error {
4
pub let message: String
5
6
init(_ message: String) {
7
self.message = message
8
}
9
}

An Error may typically be handled by failing the test case or by panicking (which will result in failing the test).

1
let err: Error? = ...
2
3
if let err = err {
4
panic(err.message)
5
}

Reading from files

Writing tests often require constructing source-code of contracts/transactions/scripts in the test script. Testing framework provides a convenient way to load programs from a local file, without having to manually construct them within the test script.

1
let contractCode = Test.readFile("./sample/contracts/FooContract.cdc")

readFile returns the content of the file as a string.