From 0c2be9517d16cb63a9a5175463ca628edb0885b9 Mon Sep 17 00:00:00 2001 From: bt3gl <1130416+bt3gl@users.noreply.github.com> Date: Thu, 10 Mar 2022 19:04:47 +0000 Subject: [PATCH] Create unit_testing.md --- solidity/unit_testing.md | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 solidity/unit_testing.md diff --git a/solidity/unit_testing.md b/solidity/unit_testing.md new file mode 100644 index 0000000..fdf7284 --- /dev/null +++ b/solidity/unit_testing.md @@ -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"); + } +} +```