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

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

相关推荐
ZDGJ609910 小时前
国际黄金期货交易趋势规则
区块链
旋生万物2 天前
全息编码:从 CRC 到 Merkle Tree 到区块链的统一视角
区块链·#人工智能·#后端开发·#软件工程·#数据结构与算法·#云原生·#分布式系统
ZDGJ60992 天前
国际期货交易中如何选择交易时机?
区块链
MuMuMu12233 天前
越华环保集团碳惠小屋:物联网 + 区块链,构建碳普惠终端数据可信与隐私保护体系
物联网·区块链
IvorySQL4 天前
PostgreSQL 日报| PgBouncer 修复三个 CVE 漏洞(10 月 15 日)
数据库·postgresql·区块链
十二同学啊4 天前
反洗钱体系详解:KYC、风险评级、交易监控与可疑交易报告
web3·区块链·智能合约
海宇大数据4 天前
零信任架构实战:基于海宇公安二要素认证即时版构建自动化证券账户实名鉴权网关
人工智能·架构·自动化·区块链
磐链科技4 天前
DApp技术架构全面解析:从区块链底层、前后端开发到链上数据交互体系设计
架构·区块链
ZDGJ60995 天前
国际期货交易时段流动性影响因素
区块链
IvorySQL5 天前
PostgreSQL 日报| pg_vectorsearch 开源向量搜索发布(10 月 14 日)
数据库·人工智能·postgresql·开源·区块链