Skip to content

Commit

Permalink
Update examples in README to reflect the latest version of the Cadenc…
Browse files Browse the repository at this point in the history
…e testing framework
  • Loading branch information
m-Peter committed Nov 3, 2023
1 parent 67c8d52 commit 39beb3d
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ Commit: 9f622977c3dff5381dbaf49fa7984805e34649d3
To install it, simply run:

```bash
sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.3.1
sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.5.0
```

## For Testing

To view code coverage results when running our tests, we can use:

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

The output will look something like this:
Expand All @@ -33,7 +33,7 @@ The output will look something like this:
Test results: "tests/test_foo_contract.cdc"
- PASS: testGetIntegerTrait
- PASS: testAddSpecialNumber
Coverage: 95.8% of statements
Coverage: 93.3% of statements
```

It looks like not all statements were covered by the test inputs. To view details for the coverage report,
Expand All @@ -42,7 +42,7 @@ we can consult the auto-generated `coverage.json` file:
```json
{
"coverage": {
"S.FooContract": {
"A.0000000000000007.FooContract": {
"line_hits": {
"14": 1,
"18": 9,
Expand All @@ -65,7 +65,7 @@ we can consult the auto-generated `coverage.json` file:
],
"statements": 15,
"percentage": "93.3%"
},
}
}
...
}
Expand All @@ -74,13 +74,13 @@ we can consult the auto-generated `coverage.json` file:
Note: We can use the `--coverprofile` flag if we wish to generate the coverage report to a different file.

```bash
flow test --cover --coverprofile=codecov.json tests/test_foo_contract.cdc
flow test --cover --covercode="contracts" --coverprofile=codecov.json tests/test_foo_contract.cdc
```

We can also generate a coverage report for the LCOV format, to be used with CI/CD plugins such as Codecov and Coveralls.

```bash
flow test --cover --coverprofile=codecov.lcov tests/test_foo_contract.cdc
flow test --cover --covercode="contracts" --coverprofile=codecov.lcov tests/test_foo_contract.cdc
```

All we need to do is give the file the `.lcov` extension.
Expand All @@ -89,7 +89,7 @@ The file will look something like this:

```bash
TN:
SF:S.FooContract
SF:A.0000000000000007.FooContract
DA:6,1
DA:14,1
DA:18,9
Expand All @@ -112,21 +112,21 @@ end_of_record

Reading the JSON/LCOV file, we can see that for `FooContract` the line `27` was missed during the tests (not covered by any of the test inputs).

To fix that, we can tweak the `testInputs` Dictionary on `tests/test_foo_contract.cdc` to observe how the coverage percentage changes. By uncommenting the line `14`, we now get:
To fix that, we can tweak the `testInputs` Dictionary on `tests/test_foo_contract.cdc` to observe how the coverage percentage changes. By uncommenting the line `23`, we now get:

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

Test results: "tests/test_foo_contract.cdc"
- PASS: testGetIntegerTrait
- PASS: testAddSpecialNumber
Coverage: 97.2% of statements
Coverage: 100.0% of statements
```

For some more realistic contracts and tests:

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

Test results: "tests/test_array_utils.cdc"
- PASS: testRange
Expand All @@ -135,18 +135,14 @@ Test results: "tests/test_array_utils.cdc"
- PASS: testMap
- PASS: testMapStrings
- PASS: testReduce
Coverage: 94.3% of statements
Coverage: 90.6% of statements
```

Look at the files `contracts/ArrayUtils.cdc` (smart contract) and `tests/test_array_utils.cdc` (tests for the smart contract).
For the `ArrayUtils.range` method, we have omitted the code branch where `start > end` on purpose. It is left as an exercise for the reader. Look at the comment on line 26 in `tests/test_array_utils.cdc`.

Note that the above examples of tests could be best described as unit tests.

An example of integration tests can be found in the `tests/test_string_utils.cdc` file, which tests the functionality of the `contracts/StringUtils.cdc` smart contract.

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

Test results: "tests/test_string_utils.cdc"
- PASS: testFormat
Expand All @@ -162,25 +158,26 @@ Test results: "tests/test_string_utils.cdc"
- PASS: testSubstringUntil
- PASS: testSplit
- PASS: testJoin
Coverage: 88.0% of statements
Coverage: 72.6% of statements
```

The generated `coverage.json` file is somewhat more elaborate, for integration tests. By viewing its content, we find the following keys:
The generated `coverage.json` file is somewhat more elaborate, for this test file. By viewing its content, we find the following keys:

- `A.01cf0e2f2f715450.ArrayUtils`
- `A.01cf0e2f2f715450.StringUtils`

and some other locations.
- `A.0000000000000007.ArrayUtils`
- `A.0000000000000007.StringUtils`

Locations that start with `A.` are contracts deployed to an account, ones that start with `s.` are scripts, and ones that start with `t.` are transactions.

The `ArrayUtils` smart contract is imported by `StringUtils`, that's why it was deployed on the integration tests, and that's why it is included in the resulting coverage report.
The `ArrayUtils` smart contract is imported by `StringUtils`, that's why it was also deployed, and
that's why it is included in the resulting coverage report.

**Note:** These two contracts are taken from: https://github.com/green-goo-dao/flow-utils. They are copied here for demonstration purposes. To get the original source code, visit the above repository.

For viewing the coverage report of the `StringUtils` smart contract, we can just consult the value of the `A.01cf0e2f2f715450.StringUtils` key, in the `coverage.json` file.
For viewing the coverage report of the `StringUtils` smart contract, we can just consult the value of the `A.0000000000000007.StringUtils` key, in the `coverage.json` file.

Note that the above examples of tests could be best described as unit tests.

There is also a more advance example of integration tests for the `ApprovalVoting` smart contract, which deals with resources, multi-sig transactions etc.
There is also a more advanced example of integration tests for the `ApprovalVoting` smart contract, which deals with resources, script execution, multi-sig transactions etc.

```bash
flow test --cover tests/test_approval_voting.cdc
Expand All @@ -196,7 +193,7 @@ Test results: "tests/test_approval_voting.cdc"
- PASS: testCastVoteOnMissingProposal
- PASS: testCastVote
- PASS: testViewVotes
Coverage: 94.1% of statements
Coverage: 82.7% of statements
```

## For Emulator
Expand Down

0 comments on commit 39beb3d

Please sign in to comment.