命令行接口
remix-tests
remix-tests
是一种命令行接口工具,用于运行Solidity单元测试。这个工具在Remix的Solidity Unit Testing
插件下作为一个库使用。在NPM上以@remix-project/remix-tests
的形式在提供。
开始
您可以使用 NPM安装它:
作为开发依赖:
npm install –save-dev @remix-project/remix-tests
作为全局NPM模块:
npm install -g @remix-project/remix-tests
若要确认已安装,请运行:
$ remix-tests version
0.1.36
版本应与NPM上的版本相同。
使用方式
您可以使用 “help” 命令看到所有可用的选项。
$ remix-tests help
Usage: remix-tests [options] [command]
Options:
-V, --version output the version number
-c, --compiler <string> set compiler version (e.g: 0.6.1, 0.7.1 etc)
-e, --evm <string> set EVM version (e.g: petersburg, istanbul etc)
-o, --optimize <bool> enable/disable optimization
-r, --runs <number> set runs (e.g: 150, 250 etc)
-v, --verbose <level> set verbosity level (0 to 5)
-h, --help output usage information
Commands:
version output the version number
help output usage information
命令的一般结构为:
$ remix-tests <options> <file/directory path>
在 “示例” 目录中运行所有测试文件
$ remix-tests examples/
在“示例”目录中运行名为 simplle_storage_test.sol
的单个测试文件
$ remix-tests examples/simple_storage_test.sol
注意: remix-tests
将假定tests(s)文件的名称以 _test.sol
为结尾。例如 simple_storage_test.sol
示例
假设有一个名为simple_storage.sol
的简单储存合约:
pragma solidity >=0.4.22 <=0.8.0;
contract SimpleStorage {
uint public storedData;
constructor() public {
storedData = 100;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}
测试文件 simplle_storage_test.sol
可以为:
pragma solidity >=0.4.22 <=0.8.0;
import "remix_tests.sol"; // injected by remix-tests
import "./simple_storage.sol";
contract MyTest {
SimpleStorage foo;
function beforeAll() public {
foo = new SimpleStorage();
}
function initialValueShouldBe100() public returns (bool) {
return Assert.equal(foo.get(), 100, "initial value is not correct");
}
function initialValueShouldNotBe200() public returns (bool) {
return Assert.notEqual(foo.get(), 200, "initial value is not correct");
}
function shouldTriggerOneFail() public {
Assert.equal(uint(1), uint(2), "uint test 1 fails");
Assert.notEqual(uint(1), uint(2), "uint test 2 passes");
}
function shouldTriggerOnePass() public {
Assert.equal(uint(1), uint(1), "uint test 3 passes");
}
}
运行 simple_storage_test.sol
文件将输出:
$ remix-tests simple_storage_test.sol
👁 :: Running remix-tests - Unit testing for solidity :: 👁
'creation of library remix_tests.sol:Assert pending...'
◼ MyTest
✓ Initial value should be100
✓ Initial value should not be200
✘ Should trigger one fail
✓ Should trigger one pass
3 passing (0.282s)
1 failing
1) MyTest: Should trigger one fail
error: uint test 1 fails
expected value to be equal to: 2
returned: 1
自定义编译器环境
大多数 remix-tests
选项都是用于自定义编译器环境的。使用扩展的自定义编译器环境,执行上述测试文件有如下输出:
$ remix-tests --compiler 0.7.4 --evm istanbul --optimize true --runs 300 simple_storage_test.sol
👁 :: Running remix-tests - Unit testing for solidity :: 👁
[14:03:18] info: Compiler version set to 0.7.4. Latest version is 0.8.0
[14:03:18] info: EVM set to istanbul
[14:03:18] info: Optimization is enabled
[14:03:18] info: Runs set to 300
Loading remote solc version v0.7.4+commit.3f05b770 ...
'creation of library remix_tests.sol:Assert pending...'
◼ MyTest
✓ Initial value should be100
✓ Initial value should not be200
✘ Should trigger one fail
✓ Should trigger one pass
3 passing (0.316s)
1 failing
1) MyTest: Should trigger one fail
error: uint test 1 fails
expected value to be equal to: 2
returned: 1
记住,自定义编译器版本需要网络连接才能加载编译器。
作为CI 的解决办法
remix-tests
也可以用于连续集成(CI)测试。
具体实现样例,请查看Su Squares contract和Travis build,其中使用了remix-tests
进行持续集成。