Skip to content

Commit

Permalink
Update the Cadence testing guide to reflect the latest release of the…
Browse files Browse the repository at this point in the history
… testing framework
  • Loading branch information
m-Peter committed Nov 8, 2023
1 parent bd19750 commit c29f5ba
Showing 1 changed file with 124 additions and 35 deletions.
159 changes: 124 additions & 35 deletions docs/build/getting-started/testing.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

# Introduction to Testing in Cadence

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.
Testing is an essential part of smart contract development to ensure the correctness and reliability of your code. The Cadence Testing Framework provides a convenient way to write tests for your contracts, scripts and transactions which allows you to verify the functionality and correctness of your smart contracts.

## Install Flow CLI

Expand All @@ -11,8 +10,7 @@ If you haven't installed the Flow CLI yet and have [homebrew](https://brew.sh/)

## 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](../../tools/flow-cli/flow.json/configuration.md).
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` config file that contains the [project's configuration](../../tools/flow-cli/flow.json/configuration.md).

```bash
mkdir test-cadence
Expand All @@ -26,80 +24,171 @@ In your code editor, create a new file called `calculator.cdc` and add the foll

```cadence calculator.cdc
access(all) contract Calculator {
access(all) fun add(a: Int, b: Int): Int {
return a + b;
access(all)
fun add(a: Int, b: Int): Int {
return a + b
}
access(all)
fun subtract(a: Int, b: Int): Int {
return a - b
}
access(all) fun subtract(a: Int, b: Int): Int {
return a - b;
access(all)
fun multiply(a: Int, b: Int): Int {
return a * b
}
}
```

## 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`.
Next up, we need to add our new contract in the `contracts` key in the `flow.json` config file. More specifically, we need to add the contract name, location and an address alias for the `testing` environment.

```json
{
"contracts": {
"Calculator": {
"source": "./calculator.cdc",
"aliases": {
"testing": "0x0000000000000007"
}
}
},
"networks": {...},
"accounts": {...},
"deployments": {...}
}
```
Enter name: Calculate
Enter contract file location: ./calculator.cdc
```

## Write unit test cases
For the time being, the address for the `testing` alias, can be one of:
- `0x0000000000000005`
- `0x0000000000000006`
- `0x0000000000000007`
- `0x0000000000000008`
- `0x0000000000000009`
- `0x000000000000000a`
- `0x000000000000000b`
- `0x000000000000000c`
- `0x000000000000000d`
- `0x000000000000000e`

In the next release, there will be `20` addresses for contract deployment during testing.

## Write unit tests

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

```cadence calculator_test.cdc
import Test
import "Calculator" // contract name from the previous step
access(all) let calculator = Calculator()
access(all) fun testAdd() {
Test.assertEqual(5, calculator.add(a: 2, b: 3))
access(all)
fun setup() {
let err = Test.deployContract(
name: "Calculator",
path: "./calculator.cdc",
arguments: []
)
Test.expect(err, Test.beNil())
}
access(all) fun testSubtract() {
Test.assertEqual(2, calculator.subtract(a: 5, b: 3))
access(all)
fun testAdd() {
Test.assertEqual(5, Calculator.add(a: 2, b: 3))
}
access(all)
fun testSubtract() {
Test.assertEqual(2, Calculator.subtract(a: 5, b: 3))
}
```

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.
This code:
- imports the `Calculator` contract from the `calculator.cdc` file (according to `flow.json`)
- deploys the `Calculator` contract to the address specified in the `testing` alias
- defines two test cases: `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:

```bash
flow test ./calculator_test.cdc
flow test --cover --covercode="contracts" calculator_test.cdc
```

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

```
Test results: "./calculator_test.cdc"
```bash
Test results: "calculator_test.cdc"
- PASS: testAdd
- PASS: testSubtract
Coverage: 66.7% of statements
```

This output indicates that both test cases ran successfully, and the two smart contract methods are functioning as expected. With the supplied flags (`--cover` & `--covercode="contracts"`), we also get code coverage insights for the contracts under testing. The code coverage percentage is `66.7%`, because we have not added a test case for the `multiply` method. By viewing the auto-generated `coverage.json` file, we see:

```json
{
"coverage": {
"A.0000000000000007.Calculator": {
"line_hits": {
"14": 0,
"4": 1,
"9": 1
},
"missed_lines": [
14
],
"statements": 3,
"percentage": "66.7%"
}
}
}
```

This output indicates that both test cases ran successfully, and the smart contract is functioning as expected.
Line 14 from the `Calculator` smart contract is marked as missed. This is the line:

```cadence
return a * b
```
which is the `multiply` method.

By adding a test case for the above method:

```cadence calculator_test.cdc
...
access(all)
fun testMultiply() {
Test.assertEqual(10, Calculator.multiply(a: 2, b: 5))
}
```

our code coverage percentage goes to `100%`:

```bash
flow test --cover --covercode="contracts" calculator_test.cdc

Test results: "calculator_test.cdc"
- PASS: testAdd
- PASS: testSubtract
- PASS: testMultiply
Coverage: 100.0% of statements
```

## Advanced Testing Techniques

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

- [**Code Coverage**](https://github.com/m-Peter/flow-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**](../../cadence/testing-framework.mdx#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**](https://github.com/bjartek/overflow): You can use [Overflow tool](https://github.com/bjartek/overflow) to run integration tests against either an local emulator, testnet, mainnet or an in memory instance of the flow-emulator.
- [**Code Coverage**](https://github.com/m-Peter/flow-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 Helpers**: Test helpers are reusable functions that help you set up the initial state for your test files. You can define test helpers in a Cadence program and use them in your test files by importing it whenever needed.
- [**Assertions**](../../cadence/testing-framework.mdx#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 files 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 helpers for all the tests in the suite.
- [**Integration tests**](https://github.com/bjartek/overflow): In our previous example, we would directly call the available methods on the contract under test. This is generally categorized as unit testing. You can also write integration tests, by executing scripts & transactions to interact with the contracts under testing. If you would like to write your tests in Go, instead of Cadence, you can use [Overflow tool](https://github.com/bjartek/overflow) 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.
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 file to verify its functionality. We then used the Flow CLI to run the test file 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.

Expand Down

0 comments on commit c29f5ba

Please sign in to comment.