Skip to content
Andrei Vlad Birgaoanu edited this page Jan 28, 2025 · 9 revisions

Tests

Tests are organized into four categories:

  1. Unit: simple tests that check the behavior of individual functions against a local EVM.
  2. Integration: similar to the unit tests but with more complex scenarios that involve multiple contracts.
  3. Invariant: conditional expressions that must always hold true.
  4. Fork: complex tests that run against an Ethereum Mainnet fork, which ensure that the protocol works with deployed ERC-20 tokens.

Unit and integration tests are further divided into two sub-categories:

  1. Concrete: standard deterministic tests that take no inputs and run the exact same test every time
  2. Fuzz: non-deterministic tests with random inputs fuzzed by Foundry

Running

You can run all tests with this command:

forge test

By default, the tests are not run against the optimized version of the contracts (i.e. the version that gets deployed to the blockchain). To do this, use the following command:

pnpm test:optimized

Filtering

To selectively filter tests by name, use the --match-test flag (or its shorthand --mt). For instance, to execute only the tests for the createWithRange function, run the following command:

forge test --match-test testFuzz_CreateWithTimestamps

Additionally, you can filter tests by contract name with the --match-contract flag (shorthand --mc). The following example demonstrates how to test the contract containing all tests for the createWithRange function:

forge test --match-contract CreateWithTimestamps

Branching Tree Technique

You may notice that some test files are accompanied by .tree files. The goal is to structure the tests within a tree in which the parent nodes represent specific state conditions that govern the smart contract's behavior, while the leaves represent the conditions being tested.

To mirror the tree in Solidity, we use modifiers following the naming pattern when<Condition> and given<Condition>

The Branching Tree Technique is explained in detail here:

Clone this wiki locally