test/TestMath.sol(合约的单元测试):
pragma solidity ^0.4.24;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Math.sol"; // 要测试的合约
// 测试合约名必须以Test开头
contract TestMath {
// 测试方法必须以test开头
function testAddFunc() public {
Math math = Math(DeployedAddresses.Math()); // Math是要测试的合约名
uint expected = 30; // 测试结果的期望值
//uint expected = 40;
Assert.equal(math.addFunc(10,20), expected, "10 + 20 should be 30!"); // 第三个参数表示出错时的提示内容。
}
}
启动truffle的开发环境(虚拟私有链):truffle develop
truffle开发环境中执行测试单元: test