ความครอบคลุมและรายงานแก๊ส
ตัวชี้วัดคุณภาพ
ความครอบคลุมและรายงานแก๊ส เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความครอบคลุมและรายงานแก๊ส”
ตัวชี้วัดคุณภาพ คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ความครอบคลุมและรายงานแก๊ส” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม
ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเขียนการทดสอบ
- การทดสอบการย้อนกลับและเหตุการณ์
- ความครอบคลุมและรายงานแก๊ส
- ฟิกซ์เจอร์