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

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

相关推荐
leijiwen1 小时前
花尖墨 Web3 水果品牌白皮书
web3
小赖同学啊2 小时前
物联网数据安全区块链服务
开发语言·python·区块链
CN-Dust2 小时前
【FMZ】数字货币程序化交易简介
区块链
数据与人工智能律师7 小时前
数字资产革命中的信任之锚:RWA法律架构的隐形密码
大数据·网络·人工智能·云计算·区块链
小赖同学啊9 小时前
区块链技术在物联网(IoT)中的核心应用场景
物联网·区块链
YSGZJJ15 小时前
股指期货空头减仓说明了什么?
区块链
小赖同学啊15 小时前
基于区块链的物联网(IoT)安全通信与数据共享的典型实例
物联网·安全·区块链
TechubNews20 小时前
东方之珠·数链未来:香港回归28周年RWA革命赋能全球金融
区块链·稳定币
yoona10201 天前
在 Sepolia 上使用 Zama fhEVM 构建隐私代币与流动性池:全流程实战指南
区块链·隐私币·zama
选择不变1 天前
日线周线MACD指标使用图文教程,通达信指标
大数据·区块链·通达信指标公式·炒股技巧·短线指标·炒股指标