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

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

相关推荐
麻雀飞吧1 小时前
期货量化主连和具体合约怎么切:天勤 KQ.m 与 KQ.i 用法
python·区块链
区块链蓝海5 小时前
$TEA将于6月4日主网启动并同步登陆多个主流平台
人工智能·区块链
带娃的IT创业者7 小时前
预测市场的至暗时刻:从西班牙封锁事件看Web3监管的技术博弈
web3·区块链·智能合约·监管合规·预测市场·去中心化应用
信徒_7 小时前
穿仓概念介绍
区块链
酿情师7 小时前
当矿池掌握 51% 算力后,会发生什么?
网络·区块链·挖矿·矿池
Upsy-Daisy9 小时前
IOTA 学习笔记(九):最小 Counter 合约在 Localnet 上的完整演示
区块链
开源量化GO9 小时前
期货量化订不到行情:天勤合约代码与交易所符号写法
python·区块链
Upsy-Daisy10 小时前
IOTA 学习笔记(十):交易与 PTB,可编程交易块怎么理解?
人工智能·区块链
cmes_love1 天前
美股和港股的量化笔记-从下载逐笔tick到合成分钟
笔记·区块链
TechubNews1 天前
數碼港「區塊鏈與數字資產試點資助計劃」︰賦能創新結算 助力穩定幣基建
区块链