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

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

相关推荐
TechubNews15 小时前
专访新火集团首席经济学家付鹏:解读比特币资产属性、香港楼市与普通人理财建议——Techub News对话实录
人工智能·区块链
王苏安说钢材A15 小时前
无锡佳钛合不锈钢有限公司不锈钢焊管厂家
区块链
财迅通Ai20 小时前
能源板块强势领涨,汇添富能源ETF(159930.SZ)单日大涨3.41%
区块链·能源·中国神华·陕西煤业
Web3VentureView1 天前
SYNBO亮相香港《前瞻》活动,联手HashKey共筑链上原生一级市场新范式
人工智能·web3·区块链·加密货币·synbo
每日综合1 天前
Web3 多链时代,安全与体验如何兼得?UKey Wallet 的“解题思路”
安全·web3·区块链
MicroTech20251 天前
微算法科技(NASDAQ :MLGO)基于量子隐形传态的区块链共识机制:量子时代下的信任重构
科技·重构·区块链
TechubNews2 天前
Base 发布首个独立 OP Stack 框架的网络升级 Azul,将是 L2 自主迭代的开端?
大数据·网络·人工智能·区块链·能源
zimoyin2 天前
Web Components 介绍与推荐三款框架
web3·html5
xiaohuoji1292 天前
震荡行情下的自动化交易:从架构视角看高抛低吸工具选型
架构·自动化·区块链
blockcoach2 天前
刘教链|比特币的马奇诺防线:从减半失灵到幂律大考
区块链