在以太坊及更广泛的区块链生态中,智能合约是自动执行、不可篡改的协议代码,构成了去中心化应用(DApps)的核心逻辑,一旦合约部署到主网,其修改成本极高,且任何漏洞都可能导致灾难性的资产损失或功能失效,在部署前对智能合约进行 rigorous(严格)的测试至关重要,而以太坊合约测试脚本正是实现这一目标的关键工具和手段。
智能合约的测试不同于传统软件测试,其特殊性在于:
测试脚本能够帮助开发者:

一个完善的以太坊合约测试脚本通常包含以下核心要素:
测试框架:

Foundry (Forge)、Hardhat (配合 Solidity 测试),允许直接用 Solidity 编写测试代码,类型安全,与合约交互紧密。Hardhat、Truffle (配合 Mocha/Chai、Jest),提供更灵活的断言库和模拟功能,适合复杂交互和前端集成测试。测试环境:
Hardhat Network、Ganache、Foundry 内置网络,它们在本地运行,快速生成测试账户和区块,模拟以太坊主网行为,但无需消耗真实 ETH。Sepolia、Goerli (虽然 Goerli 已逐渐退出,但类似概念存在),这些是与主网参数相同的公共测试网络,可以使用测试 ETH 进行真实环境下的测试。测试类型:
Foundry 的 Forge Fuzz,通过生成随机或半随机的输入数据来测试合约的鲁棒性,发现边界条件和未预期的行为。模拟与存根 (Mocking and Stubbing):

在测试中,对于依赖的外部因素(如价格预言机、时间依赖的合约),可以使用模拟对象来替代,确保测试的独立性和可重复性。
断言 (Assertions):
使用断言来验证测试结果是否符合预期,检查函数执行后状态变量的值是否正确,事件是否被正确触发,调用是否按预期回滚等。
假设我们有一个简单的 SimpleStorage 合约,用于存储一个整数。
SimpleStorage.sol 合约代码:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
测试脚本 SimpleStorage.test.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "forge-std/Test.sol";
import "../src/SimpleStorage.sol"; // 引入被测试的合约
contract SimpleStorageTest is Test {
SimpleStorage public simpleStorage;
address public owner = address(this); // 测试合约本身作为调用者
function setUp() public {
// 每个测试运行前初始化
simpleStorage = new SimpleStorage();
}
function testSetAndGet() public {
uint256 initialValue = simpleStorage.get();
assertEq(initialValue, 0, "Initial value should be 0");
uint256 newValue = 42;
simpleStorage.set(newValue);
uint256 retrievedValue = simpleStorage.get();
assertEq(retrievedValue, newValue, "Stored value should be set correctly");
}
function testSetWithDifferentValues() public {
simpleStorage.set(100);
assertEq(simpleStorage.get(), 100, "Should store 100");
simpleStorage.set(0);
assertEq(simpleStorage.get(), 0, "Should store 0");
simpleStorage.set(type(uint256).max);
assertEq(simpleStorage.get(), type(uint256).max, "Should store max uint256");
}
}
说明:
import "forge-std/Test.sol";:引入 Foundry 的测试基合约,提供 assertEq 等断言函数和测试相关工具。contract SimpleStorageTest is Test:测试合约继承自 Test.sol。function setUp() public:每个测试函数执行前都会运行,用于初始化测试环境,如部署被测试合约。function test...():以 test 开头的函数即为测试用例。assertEq(condition, expectedValue, errorMessage):断言函数,如果条件不满足,测试失败并打印错误信息。