I want to test if a function call fails using Foundry, how can I do that?

The function in question is one that spends all gas, I call it with:

level20.withdraw{gas : 10000 wei}(); and specify gas otherwise the test pend forever. With gas, the test fails and tell me that it's out of (good news, what I expected)

How can I write the test so that it doesn't fail and I'm testing that it fails?

  • Does it revert? Feb 8, 2023 at 12:24

1 Answer 1


I haven't used it specifically to test out of gas errors, but there are the expectRevert functions in forge-std/Vm.sol. Calling one of these functions will cause the vm to "catch" the revert in the following function call, and fail the test if the expected revert doesn't happen. The different variants of the function also allow you to assert that the error message is a certain value. Doing the following should work for you:

level20.withdraw{gas : 10000 wei}();
  • 1
    worked like a charm, awesome thanks!
    – foufrix
    Feb 8, 2023 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.