β How to Write Basic Tests
Test Setup
We are going to write tests for a simple counter contract:
Overwrite Counter.sol with this contract:
Write test file Counter.t.sol:

Test for error
Let's write a test case that fails. Note that setup() will be executed before executing each test case, so the counter contract in the new test case is brand new. It has nothing to do with the operations we have done in the former test case testInc().

This new test case shows "PASS" because the testFail prefix tests for failure. A failed testFail case will pass.
vm.expectRevert
We can also specify expected revert reason in the test case:
In this case we are testing for integer underflow.

Add one more test case:
Gas Report

Last updated
Was this helpful?