βœ…How to Write Basic Tests

How to Write Basic Tests

Test Setup

We are going to write tests for a simple counter contract:

First Application

Overwrite Counter.sol with this contract:

Write test file Counter.t.sol:

Test succeeded

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().

Test for error

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.

vm.expectRevert

Add one more test case:

Gas Report

Gas report

Last updated

Was this helpful?