New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

hardhat-testgen

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hardhat-testgen

Hardhat 3 plugin that helps you auto-generate unit tests for your Hardhat Solidity smart contracts

latest
Source
npmnpm
Version
1.0.7
Version published
Maintainers
1
Created
Source

Hardhat TestGen

Hardhat TestGen is a Hardhat 3 plugin that automatically generates basic unit test templates for your Solidity smart contracts.

Community plugin. Not affiliated with the Nomic Foundation. Demo version.

Features

  • Generate Solidity tests using the testgen command based on the contract ABI

  • Generate Solidity tests using the aitestgen command powered by Claude AI

Overview

Hardhat TestGen accelerates your testing workflow by generating starter test files for your Solidity contracts. It is built primarily for hackathon and prototyping use and is not yet battle-tested for complex production contracts.

  • Generates initial test structures to save setup time

  • Developers should extend and refine generated tests

  • May contain bugs or limitations

  • Currently assumes default Hardhat directory structure (does not yet support custom paths)

Installation

pnpm i -D hardhat-testgen
# or
pnpm i --save-dev hardhat-testgen

In your hardhat.config.ts, for example:

import { HardhatUserConfig } from "hardhat/config";
import hardhatTestGenPlugin from "hardhat-testgen";

export default {
  plugins: [hardhatTestGenPlugin],
  solidity: "0.8.28",
  testGenConfig: {
    anthropicApiKey: "sk-..." // Needed only for aitestgen task
  },
} satisfies HardhatUserConfig;

Usage

From your project root:

pnpm hardhat testgen --run

CLI Options:

FlagDescriptionDefault
--suppressSuppress debug logsfalse
--runRun tests after generationfalse
--skipSkip overwriting existing test filesfalse

Example:

pnpm hardhat testgen --suppress --run --skip

You can also run tests after generation:

pnpm hardhat test solidity
# or
pnpm hardhat test

AI Mode

New Feature: generate Solidity Tests with Claude AI (Anthropic)!

Currently supported model: claude-3-5-sonnet-latest.

Setup

Add it to hardhat.config.ts:

testGenConfig: {
    anthropicApiKey: "sk-..."
},

Usage

From your project root, run:

pnpm hardhat aitestgen --run

The CLI options are the same as for the testgen task (--run, --suppress, --skip).

Example

Contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

contract Counter {
    uint public x;

    event Increment(uint by);

    function inc() public {
        x++;
        emit Increment(1);
    }

    function incBy(uint by) public {
        require(by > 0, "incBy: increment should be positive");
        x += by;
        emit Increment(by);
    }
}

Generated Test:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

import { Counter } from "./Counter.sol";
import { Test } from "forge-std/Test.sol";

contract CounterTest is Test {
  Counter testContract;

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

  function test_InitialValue() public view {
    require(testContract.x() == 0, "Initial value should be 0");
  }

  function test_inc_Call() public {
    testContract.inc();
  }

  function testFuzz_Inc(uint8 n) public {
    for (uint8 i = 0; i < n; i++) {
      testContract.inc();
    }
    require(testContract.x() == n, "Value after calling inc n times should be n");
  }

  function test_incBy_ByZero() public {
    vm.expectRevert();
    testContract.incBy(0);
  }

  function test_incBy_Call() public {
    testContract.incBy(1);
  }
}

Development

pnpm install
pnpm build

Publish:

cd packages/plugin
npm publish

Run locally using the example project:

cd packages/example-project
pnpm hardhat testgen

For more information:

Disclaimer

This software is provided “as is”, without any warranties or guarantees regarding stability, security, or suitability for production use. Use at your own risk.

If you encounter issues, please report them on GitHub.

Keywords

smart-contracts

FAQs

Package last updated on 24 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts