Pisanie i uruchamianie testów kontraktów
Pisz automatyczne testy smart kontraktów, aby wykrywać błędy przed wdrożeniem ich w działającej sieci.
Pisanie i uruchamianie testów kontraktów to bezpłatna lekcja Blockchain Smart Contracts with Solidity na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Blockchain Smart Contracts with Solidity, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Blockchain Smart Contracts with Solidity zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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 coverageRunning 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 testQuick 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.
Często zadawane pytania
Czy lekcja „Pisanie i uruchamianie testów kontraktów” jest bezpłatna?
Tak — pełny tekst „Pisanie i uruchamianie testów kontraktów” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Blockchain Smart Contracts with Solidity, przejdź na CoddyKit PRO. Kurs Blockchain Smart Contracts with Solidity zawiera 4 lekcji w sumie.
Co nauczysz się w „Pisanie i uruchamianie testów kontraktów”?
Pisz automatyczne testy smart kontraktów, aby wykrywać błędy przed wdrożeniem ich w działającej sieci. Ćwiczysz Blockchain Smart Contracts with Solidity z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Blockchain Smart Contracts with Solidity?
Nie wymagamy żadnego doświadczenia. Blockchain Smart Contracts with Solidity w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Pisanie i uruchamianie testów kontraktów”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Blockchain Smart Contracts with Solidity?
Tak. Każda lekcja Blockchain Smart Contracts with Solidity zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Konfiguracja Hardhat lub Truffle
- Kompilowanie i wdrażanie kontraktów
- Interakcja z wdrożonymi kontraktami
- Pisanie i uruchamianie testów kontraktów