mirror of
https://github.com/autistic-symposium/mev-toolkit.git
synced 2025-05-02 06:46:13 -04:00
Create unit_testing.md
This commit is contained in:
parent
a905d1323b
commit
0c2be9517d
1 changed files with 48 additions and 0 deletions
48
solidity/unit_testing.md
Normal file
48
solidity/unit_testing.md
Normal file
|
@ -0,0 +1,48 @@
|
|||
## Basic Unit testing
|
||||
|
||||
A generic unit testing file looks like:
|
||||
|
||||
|
||||
```
|
||||
pragma solidity >=0.4.22 <0.8.0;
|
||||
import "remix_tests.sol"; // this import is automatically injected by Remix.
|
||||
import "remix_accounts.sol";
|
||||
// Import here the file to test.
|
||||
|
||||
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
|
||||
contract testSuite {
|
||||
|
||||
/// 'beforeAll' runs before all other tests
|
||||
/// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
|
||||
function beforeAll() public {
|
||||
// Here should instantiate tested contract
|
||||
Assert.equal(uint(1), uint(1), "1 should be equal to 1");
|
||||
}
|
||||
|
||||
function checkSuccess() public {
|
||||
// Use 'Assert' to test the contract,
|
||||
// See documentation: https://remix-ide.readthedocs.io/en/latest/assert_library.html
|
||||
Assert.equal(uint(2), uint(2), "2 should be equal to 2");
|
||||
Assert.notEqual(uint(2), uint(3), "2 should not be equal to 3");
|
||||
}
|
||||
|
||||
function checkSuccess2() public pure returns (bool) {
|
||||
// Use the return value (true or false) to test the contract
|
||||
return true;
|
||||
}
|
||||
|
||||
function checkFailure() public {
|
||||
Assert.equal(uint(1), uint(2), "1 is not equal to 2");
|
||||
}
|
||||
|
||||
/// Custom Transaction Context
|
||||
/// See more: https://remix-ide.readthedocs.io/en/latest/unittesting.html#customization
|
||||
/// #sender: account-1
|
||||
/// #value: 100
|
||||
function checkSenderAndValue() public payable {
|
||||
// account index varies 0-9, value is in wei
|
||||
Assert.equal(msg.sender, TestsAccounts.getAccount(1), "Invalid sender");
|
||||
Assert.equal(msg.value, 100, "Invalid value");
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue