Writing and Running Contract Tests
Write automated tests for your smart contracts so you can catch bugs before deploying to a live network.
Writing and Running Contract Tests is a free Blockchain Smart Contracts with Solidity lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Blockchain Smart Contracts with Solidity learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Test Smart Contracts?
Deployed contract code is immutable and often holds real value. A bug can be unfixable and costly. Automated tests let you verify behavior repeatedly and safely before any deployment.
The Test Toolkit
Hardhat and Truffle integrate test runners. Hardhat commonly uses Mocha with the Chai assertion library and ethers.js to deploy and call contracts in tests.
Test File Layout
Tests live in the test/ folder. Each file describes a contract with describe blocks and individual cases with it blocks.
describe("Counter", function () {
it("starts at zero", async function () {
// ...
});
});Deploying in a Test
Before each test you deploy a fresh contract instance so tests stay isolated and do not affect each other.
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();Making Assertions
Call a contract function and assert the result with Chai. Reading the returned value confirms the contract behaves as expected.
expect(await counter.value()).to.equal(0);
await counter.increment();
expect(await counter.value()).to.equal(1);Testing Reverts
Good tests also confirm failures. Assert that a call reverts with the expected reason when given invalid input.
await expect(counter.setOwner(addr)).to.be.revertedWith("Not owner");Testing Events
Verify that a function emits the right event with the right arguments. Events are how off-chain apps learn about state changes.
await expect(counter.increment())
.to.emit(counter, "Incremented")
.withArgs(1);Working with Multiple Accounts
Test access control by calling functions from different signers. Connect a contract to another account to simulate a non-owner trying a restricted action.
const [owner, other] = await ethers.getSigners();
await expect(counter.connect(other).reset())
.to.be.reverted;Manipulating Time and State
Test runners can fast-forward block time and mine blocks, letting you test time-locked logic without waiting. This is essential for vesting or auction contracts.
Measuring Coverage
A coverage tool reports which lines and branches your tests exercise. Aim for high coverage on critical paths, especially money-moving functions and access checks.
npx hardhat coverageRunning the Suite
Run all tests with a single command. Integrate this into CI so every change is verified automatically before merge or deploy.
npx hardhat testQuick Check
Check your contract testing knowledge.
Recap
You learned to test contracts:
- Use Mocha/Chai with ethers in the
test/folder - Deploy a fresh instance and assert return values
- Test reverts, events, and multiple accounts
- Manipulate time, measure coverage, and run in CI
A solid test suite is your best defense against costly on-chain bugs.
Frequently asked questions
Is the “Writing and Running Contract Tests” lesson free?
Yes — the full text of “Writing and Running Contract Tests” is free to read here on the web, and the Blockchain Smart Contracts with Solidity course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Blockchain Smart Contracts with Solidity course, upgrade to CoddyKit PRO.
What will I learn in “Writing and Running Contract Tests”?
Write automated tests for your smart contracts so you can catch bugs before deploying to a live network. You practise Blockchain Smart Contracts with Solidity with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Blockchain Smart Contracts with Solidity?
No prior experience is required. Blockchain Smart Contracts with Solidity on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing and Running Contract Tests” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Blockchain Smart Contracts with Solidity lesson?
Yes. Every Blockchain Smart Contracts with Solidity lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up Hardhat or Truffle
- Compiling and Deploying Contracts
- Interacting with Deployed Contracts
- Writing and Running Contract Tests