How to Add Foundry to an existing Hardhat Project (with Screenshots)

If you’re familiar with Ethereum development and Solidity, you might have heard about the Foundry test framework that has been making waves in the Smart contract development industry.

If you haven’t heard about Foundry, it’s a development framework just like Hardhat. It was developed by Dapp Tools to compete with similar frameworks like Hardhat and Brownie. The key difference is the programming language used to write tests and scripts. While Hardhat uses Javascript to write tests and deployment scripts for your smart contracts, Foundry uses the native Solidity programming language to do all of that. That’s why it has gained a lot of popularity recently.

Being able to write tests and deployment scripts in Solidity not only makes it super easy and quick to test and deploy contracts, but the speed of tests increases dramatically. It has been noted that Foundry is almost 5 times faster compared to Hardhat in terms of test execution.

In addition to that, Foundry offers Fuzz testing out of the box. Foundry Fuzzer generates random values for your tests to test for edge cases.

Is it Possible to use Foundry with an Existing Hardhat Project?

Yes, absolutely. Although, Foundry uses a different directory structure compared to Hardhat, however, it’s still possible to integrate Foundry with Hardhat. Many popular blockchain projects that already have their code-bases developed with hardhat have integrated with Foundry as an additional setup to better test their smart contracts.

And you can do the same. If you already have your project developed with Hardhat framework, you can still use Foundry with it. In fact, many projects prefer to deploy their contracts with Hardhat tooling but test their contracts with Foundry.

How to Install Foundry on your Computer

We are including these steps just in case you are installing Foundry for the first time on your PC. You will need Linux to run the installation command below. If you want to install it on Windows, you will need Windows Subsystem for Linux installed.

First, run the following command:

curl -L | bash

Then run this command to install Foundry:


How to Setup Foundry with hardhat

Here are the steps you need to follow in order to integrate Foundry with your Hardhat project:

Let’s suppose that you have a Hardhat project with dependencies such as @oppenzeppelin/contracts inside the node_modules directory.

Now, in your hardhat project, you should have the following directory structure:

  • Smart contracts are in the /contracts folder
  • Hardhat unit tests are located at the /test folder. We will put Foundry test files inside /test/foundry
  • Hardhat inserts its cache in /cache folder. We will put the Foundry cache inside the folder /forge-cache
  1. Step-1

    Create a sample Foundry project on your PC. To initiate a new project run the following command:

    forge init your_project

    Now copy the lib/forge-std from this sample project to your Hardhat project directory.


  2. Now, copy the foundry.toml configuration file to your hardhat project directory and change these variables: src, out, test, cache_path inside this file like so:foundry-hardhat-step-2
  3. Create a new file called remappings.txt in order to make Foundry project work well with the VS Code Solidity extension. Insert the following into this file:foundry-hardhat-step-3
  4. Now create a sub-directory test/foundry and write your Foundry tests in it. Just for testing purposes, copy the contract .sol file from the sample foundry project we created earlier and put it inside the /contracts folder in your main hardhat project.Also, copy the sample Counter.t.sol file from the sample project to the /test/foundry folder.Once that’s done, now you can compile the contract using command:
    forge build

    And finally you can run the Foundry test using this command:

    forge test


That’s all you need to do to add Foundry tests into your hardhat project. Hope you enjoyed it!

Spread the Word

You May Also Like

About the Author: Umair

A self-learned Javascript developer specializing in Frontend and Backend frameworks including React.js, Redux, Node.js, Express, MongoDB. He has extensive industry experience as a Tech Support lead and System Administrator. Currently learning Web3, (Solidity, Hardhat, Ethers.js) Smart contracts development, testing and auditing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.