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

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

相关推荐
ZDGJ60991 小时前
外盘期货与内盘期货交易规则差异
区块链
明月_清风4 小时前
📊 Tokenomics 入门:如何看懂一个项目的代币经济?
后端·web3
明月_清风4 小时前
🏛️ DAO 治理入门:没有老板的公司怎么运转?
后端·web3
木西20 小时前
硬核拆解 Uniswap V4:从单例架构、EIP-1153 到 Hooks 落地源码复刻
web3·智能合约·solidity
明月_清风1 天前
💰 DeFi 入门完全指南:从 Uniswap 到 Aave,一文读懂去中心化金融
后端·web3
明月_清风1 天前
🎨 NFT 全景解析:从 JPEG 到数字所有权革命
后端·web3
IvorySQL2 天前
PG 日报|优化缓冲区批量扫描,降低多套接字并发竞争
数据库·人工智能·postgresql·开源·区块链
mrbande2 天前
Vatee万腾:把信息透明度做扎实,更谨慎的使用者更容易感受到的清单
区块链
Web3李李2 天前
Web3 创业最大误区:DApp 能跑≠能上线!无数项目死在这个
金融·web3·区块链·智能合约·软件开发·dapp开发·defi开发
ithadoop2 天前
CLOB-FIRST:当永续合约把“订单簿“重新变成主角
区块链·clob·amm·永续合约·hyperliquid·dydx v4·gmx v2