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

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

相关推荐
前进的李工1 小时前
零知识证明:不泄露秘密也能自证
人工智能·web安全·区块链·零知识证明
焦点链创研究所1 小时前
以太坊基金会:以太坊状态演进路径与未来挑战
区块链
hengcaib2 小时前
赵良波:打造生鲜配送行业标杆,引领“新鲜、优质、安全”新风尚
大数据·区块链
CryptoRzz4 小时前
日本股票 API 对接实战指南(实时行情与 IPO 专题)
java·开发语言·python·区块链·maven
Wnq100725 小时前
在去中心化的边缘计算机集群中部署分布式 CORBA 及其AGENT
分布式·去中心化·区块链
berabtc7 小时前
比特币价值稳定后参与去中心化金融活动
金融·web3·去中心化·区块链·微策略·btcfi
Moonbeam Community7 小时前
Polkadot 生态的跨链桥:Snowbridge、Hyperbridge 与未来的 REVM
区块链
MicroTech20258 小时前
隐私计算与区块链融合:微算法科技(NASDAQ MLGO)构建新一代区块链网络的创新实践
网络·科技·区块链
Sui_Network8 小时前
社交游戏 Super-B 登陆 Epic 游戏商店抢先体验
人工智能·游戏·rpc·区块链·量子计算
友莘居士17 小时前
Hyperledger Fabric与 FISCO BCOS深度对比
区块链·fabric·fisco