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

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

相关推荐
ZhengEnCi11 小时前
FastAPI 项目结构完全指南-从零基础到企业级应用的 Python Web 开发利器
服务器·python·web3
闲谈共视1 天前
web3定义以及应用落地场景展望
web3
OpenBuild.xyz1 天前
Web3 开发者周刊 71 | Solar Mini Hacker House 即将席卷六城!
web3
OpenBuild.xyz1 天前
iExec 隐私工具:Web3 隐私开发 Workshop 回顾+$200 实战任务参与指南!
web3
leijiwen1 天前
什么是 Web3 品牌?
web3
dingzd951 天前
TikTok推荐算法快速解析
算法·机器学习·web3·facebook·推荐算法·tiktok·instagram
友莘居士2 天前
Java基于Web3j调用智能智能合约案例
java·web3·智能合约
CryptoRzz2 天前
越南k线历史数据、IPO新股股票数据接口文档
java·数据库·后端·python·区块链
CryptoPP3 天前
获取越南股票市场列表(包含VN30成分股)实战指南
大数据·服务器·数据库·区块链
链上日记3 天前
OKZOO亮相欧洲区块链大会:HealthFi全球化进程加速
区块链