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

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

相关推荐
The_Ticker1 天前
日股实时行情接口使用指南
java·经验分享·笔记·python·算法·区块链
三秋树1 天前
从 Solv Protocol 273 万美元被黑事件,深入理解 Solidity 自重入攻击 —— ERC-3525 标准中的暗坑
区块链
MicroTech20251 天前
微算法科技(NASDAQ: MLGO)探索量子隐形传态区块链隐私保护签名技术,增强Web 3.0元宇宙环境的效率、安全性和真实性
科技·区块链
草原猫1 天前
公链开发:从技术筑基到生态共生,重构数字信任基础设施
重构·区块链
小白的代码日记1 天前
区块链分叉检测与回扫系统(Go语言)
人工智能·golang·区块链
Blockchina1 天前
Web3项目开发全流程详解:从0到1搭建DApp架构(实战版)
架构·web3·区块链·perp dex
Blockchina2 天前
Web3金融革命:PerpDEX的深度解析
金融·web3·区块链·perp dex·永续去中心化交易所
Joy T2 天前
【Web3】深入解析Coin-Token、ERC20-ERC721、自定义错误机制与合约继承
web3·代币·自定义错误·gas·erc-20·原生币·合约继承
Joy T2 天前
【Web3】Solidity收款合约初探与去中心化预言机(Chainlink)机制解析
web3·去中心化·区块链·预言机·chainlink·don·datafeed
fuzamei8882 天前
【市场观察】黄金、白银等金属回归“真实资产”,区块链价值的时代也在路上
区块链