-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 examples added #2720
testing examples added #2720
Conversation
Aniket-Engg
commented
Apr 10, 2020
- fixes #2550
- fixes #2636
- Added testing examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the examples ( unless I'm misunderstanding), I'd like to be able to view or see the different failure reasons like method execution should fail
or failed unexpected
- but this may not be available yet in either in the unit testing module or in my understanding of what it can do now.
Let's review this. The rest of the requests are pointed to specifically.
docs/unittesting.md
Outdated
|
||
**1. Custom Compiler Context** | ||
|
||
`Solidity Unit Testing` refers `Solidity Compiler` plugin for compiler configurations. One can provide customized inputs for `Compiler`, `EVM Version` & `Enable Optimization`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got a little confused so I edited that sentence.
Solidity Unit Testing
refers to the Solidity Compiler
plugin for compiler configurations. One can provide customized inputs for Compiler
, EVM Version
& Enable Optimization
and these be the configuration settings used in the unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
docs/unittesting.md
Outdated
|
||
**2. Custom Transaction Context** | ||
|
||
For a contract method interaction, prime parameters of transaction are `from` address, `value` & `gas`. Usually, we need to test method's behaviour under different values of these parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding a 'a':
Usually, we need to test a method's
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
docs/unittesting.md
Outdated
|
||
Complete example can be seen in [examples](./unittesting_examples) section. | ||
|
||
Regarding `gas`, Remix estimate the required gas for each transaction internally. Still if a contract deployment fails with `Out-of-Gas` error, it tries to redeploy it by doubling the gas. Deployment failing with double gas will show error: `contract deployment failed after trying twice: The contract code couldn\'t be stored, please check your gas limit` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
estimate should be estimates:
"Regarding gas
, Remix estimates"
And - the backticked line is coming in wider than the column - it should probably be 3 backticks. Also the word couldn't is rendering as could\'t
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, but with 3 backticks too, it is going out of column
docs/unittesting.md
Outdated
Points to remember | ||
------------------ | ||
|
||
* A test contract can not have a method with parameters. Having one such method will show error: `Method 'methodname' can not have parameters inside a test contract` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can not -> cannot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
docs/unittesting.md
Outdated
------------------ | ||
|
||
* A test contract can not have a method with parameters. Having one such method will show error: `Method 'methodname' can not have parameters inside a test contract` | ||
* No. of test accounts are `3` before remix-ide release v0.10.0 and `10` afterwards |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should spell the word number because the abbriviation for that is different in different countries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
docs/unittesting_examples.md
Outdated
### 3. Testing method execution | ||
|
||
With Solidity, one can verify the changes made by a method in storage by accessing those variables out of a contract. But while testing whether method execution was successful and if execution failed, what was the reason behind it, can also be an importnat case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
importnat -> important
} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some questions that could equally apply to the medium post:
-
I get a little confused about the variable account1 and the
/// account-1 are they necessarily the same thing? -
And why don't you need to make an new instance of the contract in this Sender_test.sol - (as in
new Sender();
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the contract variables. Added and updated some comment to make it better explanatory. Please have a look @ryestew
docs/unittesting_examples.md
Outdated
### 3. Testing method execution | ||
|
||
With Solidity, one can verify the changes made by a method in storage by accessing those variables out of a contract. But while testing whether method execution was successful and if execution failed, what was the reason behind it, can also be an importnat case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I edited this paragraph, because I was having a hard time digesting it ( both here and in the Medium post).
With Solidity, one can directly verify the changes made by a method in storage by retrieving those variables from a contract. But testing for a successful method execution takes some strategy. Well that is not entirely true, when a test is successful - it is usually obvious why it passed. However, when a test fails, it is essential to understand why. And to help, Solidity introduced the try-catch statement in version 0.6.0. Previously, we had to use low-level calls to track down what was going on.
Here is an example test file that use both try-catch blocks and low level calls:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
event Added(string name, uint class, uint time); | ||
mapping(uint => Student) public register; // roll number => student details | ||
function add(uint rollNumber, string memory name, uint class) public returns (uint256){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be good to add an empty line here to separate the state vars from the functions. Also the state vars need indenting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. It is fixed.
// This will revert on 'require(class > 0 && class <= 12, "Invalid class");' for class '13' | ||
try ar.add(101, 'secondStudent', 13) returns (uint256 r) { | ||
Assert.ok(false, 'method execution should fail'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll ask you about this line tomorrow - about seeing the 'method execution should fail'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That Assert is symbolic. That line will be never executed with ar.add(101, 'secondStudent', 13)
throwing error. If due to any mistake in providing input for fail case, it is somehow passed then that Assert
will fail stating method execution should fail
.
Let me know your thoughts.