test-hardhat-testgen
v1.0.7
Published
Hardhat 3 plugin that helps you auto-generate unit tests for your Hardhat Solidity smart contracts
Maintainers
Readme
Hardhat TestGen
Hardhat TestGen is a Hardhat 3 plugin that automatically generates basic unit test templates for your Solidity smart contracts.
- https://hardhat.org/docs/getting-started#solidity-tests
Community plugin. Not affiliated with the Nomic Foundation. Demo version.
Features
Generate Solidity tests using the
testgencommand based on the contract ABIGenerate Solidity tests using the
aitestgencommand 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 test-hardhat-testgen
# or
pnpm i --save-dev test-hardhat-testgenIn your hardhat.config.ts, for example:
import { HardhatUserConfig } from "hardhat/config";
import hardhatTestGenPlugin from "test-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 --runCLI Options:
| Flag | Description | Default |
| ------------ | ------------------------------------ | ------- |
| --suppress | Suppress debug logs | false |
| --run | Run tests after generation | false |
| --skip | Skip overwriting existing test files | false |
Example:
pnpm hardhat testgen --suppress --run --skipYou can also run tests after generation:
pnpm hardhat test solidity
# or
pnpm hardhat testAI Mode
New Feature: generate Solidity Tests with Claude AI (Anthropic)!
Currently supported model: claude-3-5-sonnet-latest.
Setup
- Obtain Anthropic API key from: https://console.anthropic.com/settings/keys
Add it to hardhat.config.ts:
testGenConfig: {
anthropicApiKey: "sk-..."
},Usage
From your project root, run:
pnpm hardhat aitestgen --runThe 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 buildPublish:
cd packages/plugin
npm publishRun locally using the example project:
cd packages/example-project
pnpm hardhat testgenFor more information:
- https://github.com/NomicFoundation/hardhat3-plugin-template
- https://hardhat.org/plugin-development
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.
