Coverage e report del gas
Metriche di qualità
Coverage e report del gas è una lezione Web3 & DApp Development Fundamentals gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Web3 & DApp Development Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Web3 & DApp Development Fundamentals con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 29
- Lezioni
- 105
Domande Frequenti
La lezione «Coverage e report del gas» è gratuita?
Sì — il testo completo di «Coverage e report del gas» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Web3 & DApp Development Fundamentals, passa a CoddyKit PRO. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.
Cosa imparerò in «Coverage e report del gas»?
Metriche di qualità Eserciti Web3 & DApp Development Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Web3 & DApp Development Fundamentals?
Non è richiesta alcuna esperienza precedente. Web3 & DApp Development Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Coverage e report del gas»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Web3 & DApp Development Fundamentals?
Sì. Ogni lezione Web3 & DApp Development Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Scrittura dei test
- Test di revert ed eventi
- Coverage e report del gas
- Fixture