欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > remix测试文件测试智能合约

remix测试文件测试智能合约

2024/10/24 14:24:55 来源:https://blog.csdn.net/2302_77339802/article/details/140102825  浏览:    关键词:remix测试文件测试智能合约

remix内其实也是可以通过编写测试文件来测试智能合约的,需要使用插件自动生成框架以及测试结果。本文介绍一个简单的HelloWorld合约来讲解

安装插件多重检测:

(solidity unit testing)

编译部署HelloWorld合约

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;contract HelloWorld {// 定义一个映射来存储每个ID对应的问候语mapping(uint256 => string) private greetings;// 设置问候语function setGreeting(uint256 id, string memory _greet) public {greetings[id] = _greet;}// 获取问候语function getGreeting(uint256 id) public view returns (string memory) {return greetings[id];}
}

点击刚才的插件页面

点击Generate

把右边生成的代码删成

然后进行代码编写,成功如下:

// SPDX-License-Identifier: GPL-3.0pragma solidity >=0.4.22 <0.9.0;import "remix_tests.sol"; import "remix_accounts.sol";
import "../contracts/HelloWorld.sol";contract testSuite {HelloWorld helloWorld;function beforeAll() public {helloWorld = new HelloWorld();}function testOne() public{helloWorld.setGreeting(1,"hello,solidity");(string memory greet) = helloWorld.getGreeting(1);Assert.equal(string("hello,solidity"),greet,"error:the code is error");}}

补充:

  • 使用的语言是solidity语言
  • 这个文件是用于在 Remix IDE 中测试 HelloWorld 合约的功能。测试框架 remix_tests.sol 提供了一些工具和断言方法,用于验证合约的行为是否符合预期。
  • remix_accounts.sol 提供了多个账户,方便测试合约在不同账户下的行为。

至此,结束~求点赞求收藏

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com