カバレッジとGasレポート
品質指標
「カバレッジとGasレポート」はCoddyKit上の無料Web3 & DApp Development Fundamentalsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
AI チューターと学ぶ Web3 & DApp Development Fundamentals — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 29
- レッスン
- 105
よくある質問
「カバレッジとGasレポート」レッスンは無料ですか?
はい。「カバレッジとGasレポート」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web3 & DApp Development Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。
「カバレッジとGasレポート」で何を学びますか?
品質指標 ブラウザで直接実行するハンズオンコードでWeb3 & DApp Development Fundamentalsを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- テストの作成
- リバートとイベントのテスト
- カバレッジとGasレポート
- フィクスチャ