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

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

相关推荐
AIFQuant2 天前
ETF行情API接入踩坑记:从报错到跑通的七个问题
python·金融·区块链·etf·基金
磐链科技2 天前
DApp 前端开发指南:钱包连接与链上数据交互实践
区块链·智能合约
磐链科技2 天前
DApp 智能合约开发详解:Solidity 编写、测试与部署全流程
区块链
ACME20442 天前
江浙沪商铺投资胜负手
区块链
baopixiaoz3 天前
BeeQuant × BeeAgent:用AI加速策略验证
大数据·人工智能·python·区块链
木西3 天前
把 NEAR 的 “链抽象与 AI 智能体” 搬进以太坊:用 Solidity 0.8.28 打造元交易意图结算核心底座
智能合约·solidity·以太坊
磐链科技4 天前
区块链开发公司发展趋势分析:智能合约、DApp与行业数字化应用探索
区块链·智能合约
皇儒无上4 天前
智慧矿山-关于推进山西省煤矿灾害差异化智能化建设强化 AI 风险防控的政策建议
人工智能·机器学习·区块链