0Pricing
Web3 & DApp Development Fundamentals · درس

تقارير التغطية وGas

مقاييس الجودة

تقارير التغطية وGas درس مجاني في Web3 & DApp Development Fundamentals على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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-coverage

Then 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 coverage

It prints a table per file and writes an HTML report to coverage/ you can open in a browser.

npx hardhat coverage

Reading 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-coverage reports statement, branch, function, and line coverage via npx 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web3 & DApp Development Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.

ماذا ستتعلم في «تقارير التغطية وGas»؟

مقاييس الجودة تتمرن على Web3 & DApp Development Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Web3 & DApp Development Fundamentals؟

لا تُشترط خبرة سابقة. Web3 & DApp Development Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «تقارير التغطية وGas»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Web3 & DApp Development Fundamentals هذا؟

نعم. كل درس في Web3 & DApp Development Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. كتابة الاختبارات
  2. اختبار حالات Revert والأحداث
  3. تقارير التغطية وGas
  4. التركيبات
← العودة إلى Web3 & DApp Development Fundamentals