web3-starter-sol/foundry/unit-testing.md
dr. mia von steinkirch, phd fddaddb192
Update unit-testing.md
2023-02-17 18:06:34 -08:00

2.7 KiB

basic solidity unity test


assert vs. require

  • Assert() should only be used to test for internal errors, and to check invariants.
  • Require() should be used to ensure valid conditions are met that cannot be detected until execution time.
  • You may optionally provide a message for require, but not for assert.

functions


functions in a test file to make testing more structural:

  • beforeEach() - Runs before each test
  • beforeAll() - Runs before all tests
  • afterEach() - Runs after each test
  • afterAll() - Runs after all tests

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");
    }
}

note that ine can input custom values for msg.sender & msg.value of transaction using NatSpec comments.



resources