You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/forge/writing-tests.md
+54Lines changed: 54 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,60 @@ such as `Ownable.sol`'s `onlyOwner` modifier, then the test contract `0xb4c...7e
53
53
> Test functions must have either `external` or `public` visibility. Functions declared as `internal` or
54
54
> `private` won't be picked up by Forge, even if they are prefixed with `test`.
55
55
56
+
### Before test setups
57
+
58
+
Unit and fuzz tests are stateless and are executed as single transactions, meaning that the state modified by a test won't be available for a different one (instead, they'll use the same state created by `setUp` call).
59
+
It is possible to simulate multiple transactions in a single test, with a dependency tree, by implementing the `beforeTestSetup` function.
60
+
61
+
-`beforeTestSetup`: Optional function that configures a set of transactions to be executed before test.
62
+
63
+
```solidity
64
+
function beforeTestSetup(
65
+
bytes4 testSelector
66
+
public returns (bytes[] memory beforeTestCalldata)
67
+
```
68
+
69
+
where
70
+
-`bytes4 testSelector` is the selector of the test for which transactions are applied
71
+
-`bytes[] memory beforeTestCalldata` is an array of arbitrary calldata applied before test execution
72
+
73
+
> 💡 **Tip**
74
+
>
75
+
> This setup can be used for chaining tests or for scenarios when a test needs certain transactions committed before test run (e.g. when using `selfdestruct`).
76
+
> The test fails if any of the configured transaction reverts.
77
+
78
+
For example, in contract below, `testC` is configured to use state modified by `testA` and `setB(uint256)` functions:
79
+
```solidity
80
+
contract ContractTest is Test {
81
+
uint256 a;
82
+
uint256 b;
83
+
84
+
function beforeTestSetup(
85
+
bytes4 testSelector
86
+
) public pure returns (bytes[] memory beforeTestCalldata) {
0 commit comments