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

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

相关推荐
OxYGC3 小时前
[Web3] 钱包业务第十六篇: Solana 钱包链调研,钱包 SDK 开发,Staking 开发
web3·区块链·以太坊·比特币
大今野1 天前
人工智能与区块链核心考点全解析
人工智能·区块链·uml·专转本
莫叫石榴姐2 天前
现货黄金技术分析:3427破而复收之后,关键价位与下周情景推演
区块链·可信计算技术
OxYGC2 天前
[Web3] 钱包业务第十五篇: Bitcoin 链调研与钱包开发
web3·区块链·以太坊·比特币
OxYGC2 天前
[Web3] 钱包业务第十三篇: Ethereum 钱包链调研实战
web3·区块链·以太坊·比特币
大今野4 天前
2026 专转本计算机|区块链典型框架:以太坊与超级账本 Fabric 考点全解(附例题)
运维·区块链·fabric·专转本
Soari4 天前
DNaaS:用网络演算与零知识证明,让确定性网络服务可以验证
智能合约·零知识证明·确定性网络·dnaas·网络演算·risc zero
OxYGC9 天前
[Web3] 智能合约 19: NFT 知识与 Web3 求职指南
区块链·智能合约·求职招聘·nft
OxYGC10 天前
[Web3] 智能合约 18: 借贷之 Compound V2 源码解读
web3·智能合约
OxYGC10 天前
[Web3] 智能合约 00: 系列总览与目录
web3·区块链·智能合约·solidity