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

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

相关推荐
mutourend1 小时前
Algorand的State Proof(状态证明)
区块链
互联网科技看点15 小时前
LV 纪元|信任归于算法,价值源于时间
区块链
狙击主力投资工具21 小时前
实战技法:上涨多峰密集续涨
区块链
2301_776045231 天前
场内交易和场外交易的区别详解
区块链
2301_776045231 天前
数字签名的特点与区块链应用要点
区块链
Shota Kishi1 天前
解析 Solana 网络结构:通过领导者调度、验证者分布与质押集中度理解分布式区块生产
分布式·web3·去中心化·区块链
酿情师1 天前
区块链原理与技术03:P2P 网络概述与区块链中的 P2P 网络(区块链网络与跨链操作01)
网络·区块链·p2p
区块block2 天前
BCT到底有什么不一样?
人工智能·区块链
IT召唤狮2 天前
【开源项目】EasyTier — 轻量级去中心化 SD-WAN 新范式
开源·去中心化·区块链
华万通信king2 天前
智能合约与智能合同:概念辨析与技术实现差异
智能合约·海外·docusign