From 2d4429f3817605b2eb3fad3417f25d7027487640 Mon Sep 17 00:00:00 2001 From: Alex Ni <12097569+nialexsan@users.noreply.github.com> Date: Mon, 16 Oct 2023 17:03:28 -0400 Subject: [PATCH 1/4] testing intro --- .../getting-started/quickstarts/testing.md | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 docs/build/getting-started/quickstarts/testing.md diff --git a/docs/build/getting-started/quickstarts/testing.md b/docs/build/getting-started/quickstarts/testing.md new file mode 100644 index 0000000000..9313d3e616 --- /dev/null +++ b/docs/build/getting-started/quickstarts/testing.md @@ -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. + +## 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. + +## 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. +```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`: + +``` +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 +- 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: + +- [**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. + +## References + +- https://github.com/onflow/developer-grants/issues/216#issuecomment-1750673975 \ No newline at end of file From ddea8ae9584c3429e9e07a4935b0740eb50ab81e Mon Sep 17 00:00:00 2001 From: Alex <12097569+nialexsan@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:57:19 -0400 Subject: [PATCH 2/4] Update docs/build/getting-started/quickstarts/testing.md Co-authored-by: j pimmel --- docs/build/getting-started/quickstarts/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/build/getting-started/quickstarts/testing.md b/docs/build/getting-started/quickstarts/testing.md index 9313d3e616..9ccfdcf6ab 100644 --- a/docs/build/getting-started/quickstarts/testing.md +++ b/docs/build/getting-started/quickstarts/testing.md @@ -99,4 +99,4 @@ By leveraging these advanced testing techniques, you can write more robust and r ## References -- https://github.com/onflow/developer-grants/issues/216#issuecomment-1750673975 \ No newline at end of file +- [Reference documentation for Cadence testing](../../../cadence/testing-framework.mdx) \ No newline at end of file From 585a82957da54ff56aae025e19fb35731e37129f Mon Sep 17 00:00:00 2001 From: Alex Ni <12097569+nialexsan@users.noreply.github.com> Date: Mon, 23 Oct 2023 11:13:56 -0400 Subject: [PATCH 3/4] :ok_hand: address PR comments --- .../{quickstarts => }/explore-more.mdx | 0 .../{quickstarts => }/testing.md | 20 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) rename docs/build/getting-started/{quickstarts => }/explore-more.mdx (100%) rename docs/build/getting-started/{quickstarts => }/testing.md (62%) diff --git a/docs/build/getting-started/quickstarts/explore-more.mdx b/docs/build/getting-started/explore-more.mdx similarity index 100% rename from docs/build/getting-started/quickstarts/explore-more.mdx rename to docs/build/getting-started/explore-more.mdx diff --git a/docs/build/getting-started/quickstarts/testing.md b/docs/build/getting-started/testing.md similarity index 62% rename from docs/build/getting-started/quickstarts/testing.md rename to docs/build/getting-started/testing.md index 9ccfdcf6ab..1f13cbcd68 100644 --- a/docs/build/getting-started/quickstarts/testing.md +++ b/docs/build/getting-started/testing.md @@ -1,16 +1,19 @@ # 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. +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. +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. +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). + ```bash mkdir test-cadence cd test-cadence @@ -35,7 +38,7 @@ access(all) contract Calculator { ## 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`: +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`. ``` Enter name: Calculate @@ -92,11 +95,14 @@ The Cadence testing framework provides various features and techniques for writi - [**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; +- [**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. +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 [Cadence language documentation](https://cadencelang.org/) and the [Flow CLI documentation](../../tools/flow-cli/index.md). ## References -- [Reference documentation for Cadence testing](../../../cadence/testing-framework.mdx) \ No newline at end of file +- [Reference documentation for Cadence testing](../../cadence/testing-framework.mdx) \ No newline at end of file From 24c36d6ec8bb743327df8b840a10518abf19a810 Mon Sep 17 00:00:00 2001 From: Alex Ni <12097569+nialexsan@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:43:21 -0400 Subject: [PATCH 4/4] address PR comments --- docs/build/getting-started/testing.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/build/getting-started/testing.md b/docs/build/getting-started/testing.md index 1f13cbcd68..4bd26b428d 100644 --- a/docs/build/getting-started/testing.md +++ b/docs/build/getting-started/testing.md @@ -45,7 +45,7 @@ Enter name: Calculate Enter contract file location: ./calculator.cdc ``` -## Write test cases +## Write unit test cases In the same directory, create a new file called `calculator_test.cdc` and add the following code: @@ -56,11 +56,11 @@ 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) + Test.assertEqual(5, calculator.add(a: 2, b: 3)) } access(all) fun testSubtract() { - Test.assertEqual(calculator.subtract(a: 5, b: 3), 2) + Test.assertEqual(2, calculator.subtract(a: 5, b: 3)) } ``` @@ -93,16 +93,19 @@ This output indicates that both test cases ran successfully, and the smart contr The Cadence testing framework provides various features and techniques for writing comprehensive test cases. 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; +- [**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; +- [**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. 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 [Cadence language documentation](https://cadencelang.org/) and the [Flow CLI documentation](../../tools/flow-cli/index.md). + ## References -- [Reference documentation for Cadence testing](../../cadence/testing-framework.mdx) \ No newline at end of file +- [Reference documentation for Cadence testing](../../cadence/testing-framework.mdx) +- https://github.com/bjartek/overflow