-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(test): allow custom txes before unit and fuzz test #8497
Conversation
- do not unwrap func - check if `beforeTestSelectors` exists - move logic in prepare_unit_test fn - apply same logic to fuzz tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the new comments are very helpful, ty
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
crates/forge/src/runner.rs
Outdated
// Apply before test configured functions (if any). | ||
let before_test_fns: Vec<_> = | ||
self.contract.abi.functions().filter(|func| func.name.is_before_test_setup()).collect(); | ||
if before_test_fns.len() == 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should generalize this "check fn exists in abi -> call" to all the callSolDefault, can be followup
What does this give us over just having users write a regular solidity function that they call at the beginning of a test? You can always use Edit: rereading it, I'm unclear if this executes before |
@mds1 I added docs here, pls lmk if this makes sense |
In those docs, wouldn't an alternate implementation of the first code block simply be the below? The below feels more clear to read, since the // run with `forge test --isolate` if you need tx side effects like selfdestruct
contract ContractTest is Test {
uint256 a;
uint256 b;
function testA() public {
require(a == 0);
a += 1;
}
function setB(uint256 value) public {
b = value;
}
function testC() public {
testA();
setB(1);
assertEq(a, 1);
assertEq(b, 1);
}
} |
So could #1543 scenario be accomplished with isolate? |
I believe it should, cc @klkvr who implemented isolate mode to confirm |
@mds1 Is it possible to specify isolation mode within the test file, maybe even for a single test alone? Like by asserting at the beginning of a test that we are in isolation mode, or by setting it programmatically? I didn't find it in the docs. One nice thing about |
@mds1 It seems that your version of the code with isolation behaves differently from the example in the foundry book using
|
) * feat(test): allow performing txes before unit test * Changes after review: - do not unwrap func - check if `beforeTestSelectors` exists - move logic in prepare_unit_test fn - apply same logic to fuzz tests * Review: Before test is not a test kind * Changes after review: beforeTestSetup new fn signature * Remove obsolete struct from test * Update crates/forge/src/runner.rs Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de> * Changes after review: avoid executor clone * Fix Cow::Borrowed usage --------- Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
Motivation
Closes #8485
Closes #1543
Atm unit / fuzz tests are performed only against the state resulting from
setup
call, but would be nice to support configuration of other txes / state modification for individual tests. This PR adds the possibility of per unit / fuzz test configuration of before test selectors / txes.Configuration can be specified by implementing
beforeTestSetup
functionwhere
bytes4 testSelector
is the selector of the test that will run on before txes modified statebytes[] memory beforeTestCalldata
is an array of arbitrary calldatas that will be applied before test runE.g. for configuring
testC
to be executed on state modified bytestA
andsetB(uint256)
functions thebeforeTestCalldata
can be implemented asSolution