0Pricing
Blockchain Smart Contracts with Solidity · Lezione

Scrittura ed esecuzione dei test dei contract

Scriva test automatizzati per i Suoi smart contract, così potrà individuare i bug prima del deployment su una rete live.

Scrittura ed esecuzione dei test dei contract è una lezione Blockchain Smart Contracts with Solidity gratuita su CoddyKit. Questa è la lezione 4 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 Blockchain Smart Contracts with Solidity, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Why Test Smart Contracts?

Deployed contract code is immutable and often holds real value. A bug can be unfixable and costly. Automated tests let you verify behavior repeatedly and safely before any deployment.

The Test Toolkit

Hardhat and Truffle integrate test runners. Hardhat commonly uses Mocha with the Chai assertion library and ethers.js to deploy and call contracts in tests.

Test File Layout

Tests live in the test/ folder. Each file describes a contract with describe blocks and individual cases with it blocks.

describe("Counter", function () {
  it("starts at zero", async function () {
    // ...
  });
});

Deploying in a Test

Before each test you deploy a fresh contract instance so tests stay isolated and do not affect each other.

const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();

Making Assertions

Call a contract function and assert the result with Chai. Reading the returned value confirms the contract behaves as expected.

expect(await counter.value()).to.equal(0);
await counter.increment();
expect(await counter.value()).to.equal(1);

Testing Reverts

Good tests also confirm failures. Assert that a call reverts with the expected reason when given invalid input.

await expect(counter.setOwner(addr)).to.be.revertedWith("Not owner");

Testing Events

Verify that a function emits the right event with the right arguments. Events are how off-chain apps learn about state changes.

await expect(counter.increment())
  .to.emit(counter, "Incremented")
  .withArgs(1);

Working with Multiple Accounts

Test access control by calling functions from different signers. Connect a contract to another account to simulate a non-owner trying a restricted action.

const [owner, other] = await ethers.getSigners();
await expect(counter.connect(other).reset())
  .to.be.reverted;

Manipulating Time and State

Test runners can fast-forward block time and mine blocks, letting you test time-locked logic without waiting. This is essential for vesting or auction contracts.

Measuring Coverage

A coverage tool reports which lines and branches your tests exercise. Aim for high coverage on critical paths, especially money-moving functions and access checks.

npx hardhat coverage

Running the Suite

Run all tests with a single command. Integrate this into CI so every change is verified automatically before merge or deploy.

npx hardhat test

Quick Check

Check your contract testing knowledge.

Recap

You learned to test contracts:

  • Use Mocha/Chai with ethers in the test/ folder
  • Deploy a fresh instance and assert return values
  • Test reverts, events, and multiple accounts
  • Manipulate time, measure coverage, and run in CI

A solid test suite is your best defense against costly on-chain bugs.

Domande Frequenti

La lezione «Scrittura ed esecuzione dei test dei contract» è gratuita?

Sì — il testo completo di «Scrittura ed esecuzione dei test dei contract» è 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 Blockchain Smart Contracts with Solidity, passa a CoddyKit PRO. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.

Cosa imparerò in «Scrittura ed esecuzione dei test dei contract»?

Scriva test automatizzati per i Suoi smart contract, così potrà individuare i bug prima del deployment su una rete live. Eserciti Blockchain Smart Contracts with Solidity 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 Blockchain Smart Contracts with Solidity?

Non è richiesta alcuna esperienza precedente. Blockchain Smart Contracts with Solidity su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Scrittura ed esecuzione dei test dei contract»?

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 Blockchain Smart Contracts with Solidity?

Sì. Ogni lezione Blockchain Smart Contracts with Solidity 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

  1. Configurazione di Hardhat o Truffle
  2. Compilazione e deployment dei contratti
  3. Interazione con i contratti distribuiti
  4. Scrittura ed esecuzione dei test dei contract
← Torna a Blockchain Smart Contracts with Solidity