覆盖率与 Gas 报告
质量指标
覆盖率与 Gas 报告 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Quality Metrics
Two metrics help judge contract quality:
- Code coverage — how much of your Solidity is exercised by tests.
- Gas usage — how expensive your functions are to call.
Both are available as Hardhat plugins, and the Toolbox includes the gas reporter.
What Coverage Tells You
Coverage measures the percentage of statements, branches, and functions touched by your tests.
- Low coverage means untested code paths — potential hidden bugs.
- 100% coverage does not guarantee correctness, but gaps are red flags.
Installing Coverage
The solidity-coverage plugin instruments your contracts and reports coverage:
npm install --save-dev solidity-coverageThen require it in your config:
require("solidity-coverage");npm install --save-dev solidity-coverage
// in hardhat.config.js:
require("solidity-coverage");Running Coverage
Run the coverage task to execute your tests with instrumentation:
npx hardhat coverageIt prints a table per file and writes an HTML report to coverage/ you can open in a browser.
npx hardhat coverageReading a Coverage Table
The report has four columns:
- % Stmts — statements executed.
- % Branch — if/else branches taken.
- % Funcs — functions called.
- % Lines — lines reached.
Branch coverage is often the hardest to fill — make sure both sides of every condition are tested.
The Gas Reporter
The hardhat-gas-reporter (in the Toolbox) prints gas costs for each function call during tests. Enable it in config:
module.exports = {
gasReporter: {
enabled: true,
},
};It outputs a table after npx hardhat test.
module.exports = {
gasReporter: {
enabled: true,
},
};Reading the Gas Table
The gas report shows, per method:
- Min / Max / Avg gas across calls.
- # calls — how often it ran in tests.
- Deployment cost for each contract.
Functions that vary widely between min and max often have conditional logic worth examining.
Pricing in Fiat
The gas reporter can show estimated costs in real money by fetching a gas price and token price:
gasReporter: {
enabled: true,
currency: "USD",
gasPrice: 30,
}This helps you communicate the real cost of operations to stakeholders.
gasReporter: {
enabled: true,
currency: "USD",
gasPrice: 30,
}Toggling with Env Vars
Coverage and gas reporting slow tests down, so gate them behind an environment variable:
gasReporter: {
enabled: process.env.REPORT_GAS === "true",
}Run REPORT_GAS=true npx hardhat test only when you need the report.
gasReporter: {
enabled: process.env.REPORT_GAS === "true",
}Using Metrics in CI
In continuous integration you can:
- Fail the build if coverage drops below a threshold.
- Comment gas diffs on pull requests.
These guardrails keep quality from silently regressing as the codebase grows.
Outputting to a File
The gas reporter can write its table to a file for archiving or diffing in CI:
gasReporter: {
enabled: true,
outputFile: "gas-report.txt",
noColors: true,
}Disabling colors keeps the file readable. Coverage similarly produces a coverage/ HTML report and an lcov.info file.
gasReporter: {
enabled: true,
outputFile: "gas-report.txt",
noColors: true,
}Quick Check
Test your understanding of coverage and gas reports.
Recap
You learned to measure test and gas quality.
solidity-coveragereports statement, branch, function, and line coverage vianpx hardhat coverage.- Branch coverage best reveals untested paths.
- The gas reporter (in the Toolbox) prints per-method gas and deployment costs.
- It can price calls in fiat and be toggled with an env var.
- Enforce thresholds in CI to prevent regressions.
常见问题解答
「覆盖率与 Gas 报告」课时是免费的吗?
是的 — 「覆盖率与 Gas 报告」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「覆盖率与 Gas 报告」这节课中我会学到什么?
质量指标 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「覆盖率与 Gas 报告」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。