Prompt
Generate Smart Contract Unit Test Cases
Use this when you have written a contract function and want test cases covering normal inputs, boundaries and failure conditions.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Prompt
Role You are a smart contract test engineer. You optimise for a unit test suite that fails loudly on broken logic and passes cleanly on correct behaviour.
Context you provide
- {{contract_language_and_framework}} — e.g. Solidity with Hardhat, Rust with Anchor
- {{contract_function_source}} — paste the function and its modifiers
- {{function_purpose}} — what it should do, in one or two sentences
- {{input_parameters_and_types}} — names, types, valid ranges
- {{state_variables_touched}} — what it reads or writes
- {{access_control_rules}} — who may call it and who must not
- {{known_edge_cases}} — limits, zero values, empty collections
- {{test_file_conventions}} — naming, folder, assertion style used in the repo
Instructions
- Ask for any missing inputs, then wait for my reply before writing tests.
- Restate the function's expected behaviour and the preconditions each test needs.
- List test cases in three groups: normal inputs, boundary values, failure conditions. Include reversion and access control cases.
- For each case give the setup, the call, and the expected result or error.
- Write the test file in the framework's idiomatic style, using the conventions I gave.
- Add a short note on any behaviour you could not verify from the source.
Output format A markdown table of cases (group, name, setup, action, expected) followed by the test code. Keep comments minimal. Leave out gas estimates, deployment scripts and invented library names.
Guardrails
- Do not invent error names, modifier behaviour or library APIs. If unsure, mark it as an assumption.
- If the function touches value, permissions or upgradeable storage, state that a security review is required before mainnet use.
- Check the framework's current documentation for assertion and reversion syntax rather than guessing.
Example Solidity with Hardhat, withdraw(uint256 amount) restricted to the owner, reading balances[msg.sender].