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

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

相关推荐
许强0xq25 分钟前
Robinhood的再进化:从零佣金交易到链上金融超级应用
金融·web3·区块链·智能合约·solidity·dapp·去平台化时代
粟悟饭&龟波功1 天前
【区块链】二、案例1:汽车供应链区块链
区块链
许强0xq1 天前
稳定币市场格局重构:分发权正在成为新的护城河!
金融·web3·区块链·dapp·稳定币·usdt·usdc
光影少年1 天前
WEBNN是什么,对前端工程带来哪些优势
前端·web3·web
Ashlee_code1 天前
经纪柜台系统解析:从今日国际金融动荡看证券交易核心引擎的变革
python·架构·系统架构·区块链·vim·柜台·香港券商
wangchenggong19882 天前
详解Solidity中的事件
区块链
会跑的葫芦怪2 天前
区块链开发与核心技术详解:从基础概念到共识机制实践
go·区块链
区块链小八歌2 天前
Kodiak Perps:Berachain 原生永续合约平台上线
区块链
MicroTech20252 天前
微算法科技(NASDAQ MLGO)“自适应委托权益证明DPoS”模型:重塑区块链治理新格局
科技·算法·区块链
天涯学馆2 天前
Solidity自毁合约:让你的区块链代码优雅谢幕
智能合约·solidity·以太坊