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

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

相关推荐
mutourend1 天前
以太坊交易类型综述
区块链
搬砖魁首2 天前
密码学系列 - 零知识证明(ZKP) - Schnorr协议
区块链·密码学·零知识证明·schnorr·fiat-shamir
MQLYES2 天前
04-BTC-协议
区块链
m0_603888712 天前
Decentralized Autoregressive Generation
ai·去中心化·区块链·论文速览
反向跟单策略3 天前
期货反向跟单—高频换人能够提高跟单效率?
大数据·人工智能·学习·数据分析·区块链
知识分享小能手3 天前
Ubuntu入门学习教程,从入门到精通,Ubuntu 22.04 中的区块链 —— 知识点详解 (23)
学习·ubuntu·区块链
电报号dapp1193 天前
钱包开发:在虚无中为数字自我筑巢
游戏·去中心化·区块链·智能合约
数据大魔方3 天前
【期货量化入门】期权交易入门:从零开始学期权量化(TqSdk完整教程)
数据库·python·mysql·算法·区块链·程序员创富
期货资管源码3 天前
期货资管分仓软件开发/平台搭建经验分享
经验分享·算法·eclipse·区块链
Rockbean4 天前
3分钟Solidity: 11.1 重入攻击
web3·智能合约·solidity