0Pricing
Blockchain Smart Contracts with Solidity · Lektion

Contract-Tests schreiben und ausführen

Schreiben Sie automatisierte Tests für Ihre Smart Contracts, um Fehler vor dem Deployment in ein Live-Netzwerk zu erkennen

Contract-Tests schreiben und ausführen ist eine kostenlose Blockchain Smart Contracts with Solidity-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Blockchain Smart Contracts with Solidity-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Contract-Tests schreiben und ausführen“ kostenlos?

Ja — der vollständige Text von „Contract-Tests schreiben und ausführen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Blockchain Smart Contracts with Solidity-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Contract-Tests schreiben und ausführen“?

Schreiben Sie automatisierte Tests für Ihre Smart Contracts, um Fehler vor dem Deployment in ein Live-Netzwerk zu erkennen Du übst Blockchain Smart Contracts with Solidity mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Blockchain Smart Contracts with Solidity zu starten?

Keine Vorkenntnisse erforderlich. Blockchain Smart Contracts with Solidity auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Contract-Tests schreiben und ausführen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Blockchain Smart Contracts with Solidity-Lektion Code schreiben und ausführen?

Ja. Jede Blockchain Smart Contracts with Solidity-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Hardhat oder Truffle einrichten
  2. Contracts kompilieren und deployen
  3. Mit deployten Contracts interagieren
  4. Contract-Tests schreiben und ausführen
← Zurück zu Blockchain Smart Contracts with Solidity