Foundry usage (2) --forge command line

Other related content can be found on: Personal homepage

2. Forge:

Forge is a command line tool included with Foundry, used to test, build and deploy smart contracts.

forge test

Run test cases, all tests are written in Solidity

Forge will look for tests anywhere in the source directory, any contract with a function starting with test is considered a test.

Usually the test is placed insrc/test中

Run specific tests by passing a filter:

forge test --match-contract ComplicatedContractTest --match-test testDeposit

testDepositThis will run the tests in the test contract with a in the name ComplicatedContractTest.

2.1 Writing tests:

The test code is written in Solidity, and the most common test writing is implemented through the contracts Forgeof the **** standard library Test.

Using the Forge standard library, you will make use of the DSTest contract, which provides basic logging and assertion functions.

Import forge-std/Test.soland inherit from test contractTest

import "forge-std/Test.sol";

A test case:

pragma solidity 0.8.10;

import "forge-std/Test.sol";

contract ContractBTest is Test {
    uint256 testNumber;

    function setUp() public {
        testNumber = 42;
    }

    function testNumberIs42() public {
        assertEq(testNumber, 42);
    }

    function testFailSubtract43() public {
        testNumber -= 43;
    }
}
  • setUp(): Optional function called before each test case is run
  • test(): testFunctions prefixed with are executed as test cases
  • testFail(): testThe opposite situation, if the function does not report an error revert, then the test fails

The test function must have externalor publicotherwise the test function will have no effect

2.2 cheatcodes

In order to manipulate the state of the blockchain, as well as test specific revertsand events Events, Foundry comes with a set of cheatcodes

Cheatcode can be accessed through instances Testprovided by contracts in the Forge standard library .vm

Let’s explain in detail with an example:

Our purpose is to verify that the function of a contract can only be called by the contract owner, write a test

Add an Owner.t.sol test file under ./testthe folder

pragma solidity 0.8.10;
import "forge-std/Test.sol";
error Unauthorized();

contract OwnerUpOnly {
    address public immutable owner;
    uint256 public count;
    constructor() {
        owner = msg.sender;
    }
    function increment() external {
        if (msg.sender != owner) {
            revert Unauthorized();
        }
        count++;
    }
}

contract OwnerUpOnlyTest is Test {
    OwnerUpOnly upOnly;
    function setUp() public {
        upOnly = new OwnerUpOnly();
    }
    function testIncrementAsOwner() public {
        assertEq(upOnly.count(), 0);
        upOnly.increment();
        assertEq(upOnly.count(), 1);
    }
}

Run forge testand find that the test passes

Next test that someone who is not the owner cannot increment the count

OwnerUpOnlyTestAdd a function to the contract :

function testIncrementAsNotOwner() public {
        vm.prank(address(0));
        upOnly.increment();
    }

Run it again forge testand find that the test revert indicates that the count cannot be increased if you are not the owner of the contract.

forge test example

**vm.prank(address)**After cheatcode changes the identity of msg.sender to the zero address, make the next call to ensure that the caller is not the contract owner.

A complete detailed cheatcodeintroduction can be found [ Cheatcodes Reference - Foundry Chinese Document (learnblockchain.cn) ]

2.3 Forge standard library overview

Forge StdProvides all the basic functionality needed to write test code

  • Vm.sol: The latest cheat code interface
  • console.soland console2.sol: Hardhat style logging functionality
  • Script.sol: Basic utilities for Solidity scripts
  • Test.sol: A superset of DSTest, including the standard library, cheat code examples ( vm) and Hardhat console

2.4 Understanding Traces

Forge can generate traces for failed tests ( -vvv) or for all tests ( )-vvvvTraces

Tracesof different colors

  • Green : for calls that do not revert
  • Red : used for calls with revert
  • Blue : used to invoke cheat codes
  • Cyan : used to trigger logs
  • Yellow : used for contract deployment
    forge trace example

2.5 Fork testing

Forge supports two different methods for forked testing:

  • Forking Mode: forge test --fork-urlUses a single fork for all tests via the standard
  • Forking Cheatcodes: Create, select, and manage multiple forks directly in the Solidity test code through forking cheat codes

2.5.1 Fork mode:

Specify the forked block height by --fork-urlpassing the RPC URL--fork-block-number

forge test --fork-url "https://mainnet.infura.io/v3/10973852e3ce414296d70fd551402e92" --fork-block-number 17001200

2.5.2 Fork cheat code:

Programmatically enter forking mode in Solidity test code.

In the Foundry test code: all test functions are isolated, each test function setup()is executed using the subsequent copy state, and setup()the branches created during the period can be used for testing.

  • createFork('mainnet', blocknumber)cheatcode creates a branch and returns a unique identifier
  • selectFork(Forkid)Pass Forkid to enable the corresponding branch
  • activeFork()Returns the Forkid of the currently enabled branch
  • rollFork(blocknumber)Set the forked block height

Each fork is an independent EVM and all forks use completely independent storage, but msg.senderthe state of and the test contract itself are persistent across fork changes

Guess you like

Origin blog.csdn.net/m0_53689197/article/details/134215687