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 examples added #2720

Merged
merged 4 commits into from
Apr 14, 2020
Merged

testing examples added #2720

merged 4 commits into from
Apr 14, 2020

Conversation

Aniket-Engg
Copy link
Collaborator

  • fixes #2550
  • fixes #2636
  • Added testing examples

Copy link
Collaborator

@ryestew ryestew left a 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.


**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`.
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated


**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.
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


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`
Copy link
Collaborator

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

Copy link
Collaborator Author

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

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`
Copy link
Collaborator

Choose a reason for hiding this comment

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

can not -> cannot

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

------------------

* 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
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

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

Choose a reason for hiding this comment

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

importnat -> important

}
```

Copy link
Collaborator

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(); )

Copy link
Collaborator Author

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

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

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:

Copy link
Collaborator Author

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){
Copy link
Collaborator

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.

Copy link
Collaborator Author

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');
Copy link
Collaborator

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'

Copy link
Collaborator Author

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.

@yann300 yann300 merged commit 4abab07 into master Apr 14, 2020
@yann300 yann300 deleted the testing-examples branch April 14, 2020 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update unit testing docs for natspec Document the custom sender & value feature for remix-tests
3 participants