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

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

相关推荐
DO_Community11 小时前
DigitalOcean携手Persistent达成战略合作,让 AI 更亲民、更易扩展
大数据·人工智能·ai·llm·区块链
AI_菲姐15 小时前
未来的 Web3 组织,将从会议开始
大数据·经验分享·web3·去中心化·区块链
MQLYES15 小时前
10-BTC-分叉
区块链
旺仔Sec16 小时前
2026年中银杯湖北省职业院校技能大赛高职组区块链技术应用任务书
运维·区块链
MQLYES1 天前
09-BTC-比特币脚本
区块链
CryptoPP1 天前
主流国际股票行情API接口横向对比:如何选择适合你的数据源?
大数据·笔记·金融·区块链
公链开发1 天前
2026 GameFi开发实战:从“玩法优先”到香港合规落地,一套可持续链游技术方案
区块链
CryptoRzz1 天前
如何高效对接美股实时行情?StockTV API 实战集成指南
java·python·flask·区块链·maven·symfony
终端域名2 天前
虚拟货币域名:加密货币与域名的碰撞 —— 构建未来互联网新生态
microsoft·区块链·加密货币·数字货币域名·网站域名
BlockChain8882 天前
Web3 后端面试专用版
java·面试·职场和发展·go·web3