Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing intro #410

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions docs/build/getting-started/quickstarts/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

# Introduction to Testing in Cadence

Testing is an essential part of smart contract development to ensure the correctness and reliability of your code. In Cadence, the testing framework provides a convenient way to write tests for Cadence programs, allowing you to verify the functionality of your smart contracts.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Testing is an essential part of smart contract development to ensure the correctness and reliability of your code. In Cadence, the testing framework provides a convenient way to write tests for Cadence programs, allowing you to verify the functionality of your smart contracts.
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.


## Install Flow CLI

The [Flow CLI](../../../tools/flow-cli/index.md) is the primary tool for developing, testing, and deploying smart contracts written in Cadence to the Flow network. You can install the Flow CLI by following the [installation instructions](../../../tools/flow-cli/install.md) provided by the Flow documentation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The [Flow CLI](../../../tools/flow-cli/index.md) is the primary tool for developing, testing, and deploying smart contracts written in Cadence to the Flow network. You can install the Flow CLI by following the [installation instructions](../../../tools/flow-cli/install.md) provided by the Flow documentation.
The [Flow CLI](../../../tools/flow-cli/index.md) is the primary tool for developing, testing, and deploying smart contracts to the Flow network.
If you haven't installed the Flow CLI yet and have [homebrew](https://brew.sh/) installed, simply run `brew install flow-cli`. Alternatively, refer to the Flow CLI [installation instructions](../../../tools/flow-cli/install.md).


## 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice: for starting a new project prefer using 'flow setup'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`flow setup' create an empty cadence project with cadence folder
it will require more explanation why there are multiple subfolders

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short explanation of what flow.json is and configures with a link to reference. 🙏🏼
Additionally we might need a page with better reference of flow.json and it's available configuration

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a reference to tools/flow-cli/flow.json/configuration.md

```bash
mkdir test-cadence
cd test-cadence
flow init
```

## Write a simple smart contract

In your code editor, create a new file called `calculator.cdc` and add the following code:

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

access(all) fun subtract(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`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick explainer: "This command adds your contract name and location under the contracts key in flow.config..."


```
Enter name: Calculate
Enter contract file location: ./calculator.cdc
```

## Write test cases

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(calculator.add(a: 2, b: 3), 5)
}

access(all) fun testSubtract() {
Test.assertEqual(calculator.subtract(a: 5, b: 3), 2)
}

```

This code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noice 🚀

- 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.

## Running the test cases

To run the test cases, use the following command in the terminal:

```bash
flow test ./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"
- PASS: testAdd
- PASS: testSubtract
```

This output indicates that both test cases ran successfully, and the smart contract is functioning as expected.

## Advanced Testing Techniques

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very close to coming to life, onflow/flow-cli#1227 just waiting for this 🙏


- [**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`, `assertTrue`, and `assertFalse`, 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.

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. This is a basic example, and there are many more advanced features and techniques you can explore when working with the Cadence testing framework. For more in-depth tutorials and documentation, refer to the official Flow Developer Portal and the Flow CLI documentation.
gregsantos marked this conversation as resolved.
Show resolved Hide resolved

## References

- [Reference documentation for Cadence testing](../../../cadence/testing-framework.mdx)