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
I wrote a test contract with 3 tests - 1 should always pass, 2 should always fail. In practice, the test that should always pass actually fails sometimes and passes other times, with no code changes (e.g. behaves non-deterministically).
Of the 2 always-failing tests, one of them calls vm.revertTo(snapshot) - could this be causing the strange behavior? To test this hypothesis, I commented out the vm.revertTo(snapshot) call, and the test suite behaved as expected deterministically (1 always passing, 2 always failing).
Test file is included below. Tests are
test_shouldPass (should be always-passing, but in practice passes/fails non-deterministically)
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Test} from "forge-std/Test.sol";
contract Target {
function num() public pure returns (uint256) {
return 2;
}
}
contract RevertTest is Test {
uint256 snapshot;
Target targ;
function setUp() public {
snapshot = vm.snapshot();
targ = new Target();
}
// this non-deterministically fails sometimes and passes sometimes
function test_shouldPass() public {
assertEq(2, targ.num());
}
// always fails
function test_shouldFaillWithRevertTo() public {
assertEq(3, targ.num());
vm.revertTo(snapshot);
}
// always fails
function test_shouldFail() public {
assertEq(3, targ.num());
}
}
anna-carroll
changed the title
Test that should pass non-deterministically fails or passes (test suite calls vm.revertTo)
Test non-deterministically fails or passes in test suite calling vm.revertTo
Nov 18, 2023
anna-carroll
changed the title
Test non-deterministically fails or passes in test suite calling vm.revertTo
Test non-deterministically fails or passes in test suite with vm.revertTo
Nov 18, 2023
anna-carroll
changed the title
Test non-deterministically fails or passes in test suite with vm.revertTo
Test non-deterministically fails in test suite with vm.revertTo
Nov 18, 2023
Component
Forge
Have you ensured that all of these are up to date?
What version of Foundry are you on?
forge 0.2.0 (c948388 2023-11-18T00:19:51.373226000Z)
What command(s) is the bug in?
forge test
Operating System
macOS (Apple Silicon)
Describe the bug
I wrote a test contract with 3 tests - 1 should always pass, 2 should always fail. In practice, the test that should always pass actually fails sometimes and passes other times, with no code changes (e.g. behaves non-deterministically).
Of the 2 always-failing tests, one of them calls
vm.revertTo(snapshot)
- could this be causing the strange behavior? To test this hypothesis, I commented out thevm.revertTo(snapshot)
call, and the test suite behaved as expected deterministically (1 always passing, 2 always failing).Test file is included below. Tests are
test_shouldPass
(should be always-passing, but in practice passes/fails non-deterministically)test_shouldFail
(always-failing)test_shouldFailWithRevertTo
(always-failing, calls vm.revertTo)Here is the entire test file:
here is a trace of a run where all three fail:
and here is a trace of a run where it passed:
The text was updated successfully, but these errors were encountered: