编写与运行合约测试
为智能合约编写自动化测试,以便在部署到真实网络之前发现错误。
编写与运行合约测试 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
用 AI 导师学习 Blockchain Smart Contracts with Solidity — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「编写与运行合约测试」课时是免费的吗?
是的 — 「编写与运行合约测试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
「编写与运行合约测试」这节课中我会学到什么?
为智能合约编写自动化测试,以便在部署到真实网络之前发现错误。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Blockchain Smart Contracts with Solidity 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「编写与运行合约测试」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?
能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 设置 Hardhat 或 Truffle
- 编译与部署合约
- 与已部署的合约交互
- 编写与运行合约测试