コントラクトテストの作成と実行
スマートコントラクトをライブネットワークにデプロイする前にバグを発見できるよう、自動テストを作成します。
「コントラクトテストの作成と実行」はCoddyKit上の無料Blockchain Smart Contracts with Solidityレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはBlockchain Smart Contracts with Solidity学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「コントラクトテストの作成と実行」レッスンは無料ですか?
はい。「コントラクトテストの作成と実行」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Blockchain Smart Contracts with Solidityコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。
「コントラクトテストの作成と実行」で何を学びますか?
スマートコントラクトをライブネットワークにデプロイする前にバグを発見できるよう、自動テストを作成します。 ブラウザで直接実行するハンズオンコードでBlockchain Smart Contracts with Solidityを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Blockchain Smart Contracts with Solidityを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのBlockchain Smart Contracts with Solidityは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「コントラクトテストの作成と実行」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このBlockchain Smart Contracts with Solidityレッスンでコードを書いて実行できますか?
はい。すべてのBlockchain Smart Contracts with Solidityレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- HardhatまたはTruffleの設定
- コントラクトのコンパイルとデプロイ
- デプロイ済みコントラクトとの対話
- コントラクトテストの作成と実行