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.0
        
pragma 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 提供了多个账户,方便测试合约在不同账户下的行为。

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

相关推荐
超级架构师2 小时前
【区块链技术】区块链101:比特币是什么?
人工智能·区块链·比特币
维克兜率天4 小时前
4.3.2.1 日常监控:上线只是开始
服务器·数据库·python·区块链·php·量化
磐链科技2 天前
交易所开发核心:钱包提现引擎的设计——从风控拦截、多级审批到自动广播上链
web3·区块链
2601_962077982 天前
阿里二面,前端开发在web3.0中该如何应用,记录面经
区块链·前端开发·web3.0·技术趋势·学习准备
baopixiaoz2 天前
BeeQuant × BeeAgent:AI驱动智能交易
大数据·人工智能·python·区块链
ZDGJ60993 天前
恒指期货什么意思,恒指期货怎么看
区块链
综合简报3 天前
东南亚发稿多少钱一篇?预算怎么规划?如何选到高性价比服务商?
区块链
磐链科技3 天前
区块链开发入门实战:用 Hardhat 与 Ethers.js 搭建你的第一个 DApp
开发语言·javascript·区块链
磐链科技3 天前
Web3钱包开发中的交易风控体系:模拟执行、沙盒隔离与授权拦截
web3·区块链
liuze4083 天前
区块链连接调用完成数据上链
区块链