> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/how-to-write-basic-tests.md).

# How to Write Basic Tests

{% embed url="<https://youtu.be/HA0GWauMOsU>" %}
How to Write Basic Tests
{% endembed %}

## Test Setup

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

{% embed url="<https://solidity-by-example.org/first-app/>" %}
First Application
{% endembed %}

Overwrite `Counter.sol` with this contract:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Counter {
    uint public count;

    // Function to get the current count
    function get() public view returns (uint) {
        return count;
    }

    // Function to increment count by 1
    function inc() public {
        count += 1;
    }

    // Function to decrement count by 1
    function dec() public {
        // This function will fail if count = 0
        count -= 1;
    }
}

```

Write test file `Counter.t.sol`:

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../src/Counter.sol";

contract CounterTest is Test {
    Counter public counter;

    function setUp() public {
        counter = new Counter();
    }

    function testInc() public {
        counter.inc();
        assertEq(counter.count(), 1);
    }
}
```

<figure><img src="/files/qvOPCKpyYXThCDO1ixmT" alt=""><figcaption><p>Test succeeded</p></figcaption></figure>

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

```solidity
function testFailDec() public {
    counter.dec();
}
```

<figure><img src="/files/FyQLBC9pLmjVsMTn2gOD" alt=""><figcaption><p>Test for error</p></figcaption></figure>

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:

```solidity
function testDecUnderflow() public {
    vm.expectRevert(stdError.arithmeticError);
    counter.dec();
}
```

In this case we are testing for integer underflow.

<figure><img src="/files/WVbwotuFD7YUsymkbXWq" alt=""><figcaption><p>vm.expectRevert</p></figcaption></figure>

Add one more test case:

```solidity
function testDec() public {
    counter.inc();
    counter.inc();
    counter.dec();
    assertEq(counter.count(), 1);
}
```

## Gas Report

```bash
forge test --match-path test/Counter.t.sol --gas-report
```

<figure><img src="/files/aPHZOaflIl8KXpzDm4eb" alt=""><figcaption><p>Gas report</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/how-to-write-basic-tests.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
