βœ…Error

Error

Setup

Target contract:

Test file:

vm.expectRevert()

If we want a test case to simply revert, use testFail prefix in the function name:

Another way of doing the same thing is to use test prefix in the function name and vm.expectRevert() before that operation:

Test require error message

If we want to test for a require() statement's error message, such as the following:

use vm.expectRevert(bytes(error_message)):

Custom error

If we want to test for custom error instead, such as the following:

use vm.expectRevert(Error.custom_error_name.selector):

Label assertions

If we have a lot of assertions in a test case, Foundry won't tell us which ones succeed and which ones fail. For example:

The 4th assertion clearly would fail but Foundry won't tell us the failed one is the 4th assertion. In order to distinguish assertions, we can label them with different names:

Last updated

Was this helpful?