本文介绍了Brownie测试索引错误:列表索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Macbook Pro:蒙特雷

英特尔酷睿i7

Brownie v1.17.2

Ganache CLI v6.12.2(Ganache-core:2.13.2)

我正在根据参考(https://www.youtube.com/watch?v=M576WGiDBdQ&t=25510s)学习固定性。

我使用Brownie框架编写并部署了一个智能契约(脚本/ploy.py),它起作用了。

然后尝试编写一个测试脚本(test/test_Simple_storage.py),该脚本向我提供终端中的错误信息。

已对此进行谷歌搜索,并尝试删除Brownie项目的Build文件夹中的所有文件,然后再次部署协定(脚本/ploy.py),这是相同的错误。

并尝试将帐户[0]的私钥和索引更改为帐户[-1]和帐户[1],结果相同。

仅供参考:我在终端中输入了命令&ganache-cli-definistic";。因此帐户和私钥不是随机的。

将Ganache帐户[0]的私钥保存在.env文件中,如下所示

.env

export PRIVATE_KEY=0x91114a07f248a1c50951cb11557af5424cc6a49bf61521874c9ae3f4ae239a6d

错误信息:

(base) liwei@liweideMacBook-Pro Brownie_Simple_Storage % brownie test
Brownie v1.17.2 - Python development framework for Ethereum

========================================================== test session starts ===========================================================
platform darwin -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/liwei/Desktop/demos/practice/Brownie_Simple_Storage
plugins: eth-brownie-1.17.2, xdist-1.34.0, hypothesis-6.27.3, web3-5.25.0, forked-1.3.0
collected 0 items / 1 error

================================================================= ERRORS =================================================================
_____________________________________________ ERROR collecting tests/test_simple_storage.py ______________________________________________
tests/test_simple_storage.py:5: in <module>
    account = accounts[0]
E   IndexError: list index out of range
======================================================== short test summary info =========================================================
FAILED tests/test_simple_storage.py - IndexError: list index out of range
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================ 1 error in 0.25s ============================================================
(base) liwei@liweideMacBook-Pro Brownie_Simple_Storage %

测试script--";tests/test_simple_storage.py"

from brownie import accounts, SimpleStorage


# Arrange
account = accounts[0]
print(account)
# Act
simple_storage = SimpleStorage.deploy({"from": account})
starting_value = simple_storage.retrieve()
expected = 0
# Assert
assert starting_value == expected

智能合同部署良好,使用相同的本地区块链,ganache-cli"脚本/部署.py"

from brownie import accounts, config, SimpleStorage
import os


def deploy_simple_storage():

    # load from you set encrypted , not from ganache-cli which is brownie automated connceted to
    # account = accounts.load("MG515-account")
    # print(account)

    # add private key use enviroment variables
    # account = accounts.add(os.getenv("PRIVATE_KEY"))
    # print(account)

    # .deploy() , always need a "from"key in a dictinary when making a transaction
    account = accounts.add(config["wallets"]["from_key"])
    simple_storage = SimpleStorage.deploy({"from": account})
    stored_value = simple_storage.retrieve()
    print("Current stored value is :")
    print(stored_value)
    print("Updating Contract...")
    transaction = simple_storage.store(15, {"from": account})
    transaction.wait(1)
    updated_store_value = simple_storage.retrieve()
    print("Current stored value is :")
    print(updated_store_value)
    print(account)


def main():
    deploy_simple_storage()

更新于20220111

阅读Brownie文档找到此命令";network.connect(";development";)";后,此命令设置网络是否正确,我想起Brownie应该默认连接到开发网络。

因此更新了代码,如下所示

from brownie import accounts, SimpleStorage, network


network.connect("development")

# Arrange
account = accounts[0]

print(accounts)
# Act
simple_storage = SimpleStorage.deploy({"from": account})
starting_value = simple_storage.retrieve()
expected = 0
# Assert

assert starting_value == expected

和终端发出一条警告信息

(base) liwei@liweideMacBook-Pro Brownie_Simple_Storage % brownie test
Brownie v1.17.2 - Python development framework for Ethereum

========================================================== test session starts ==========================================================
platform darwin -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/liwei/Desktop/demos/practice/Brownie_Simple_Storage
plugins: eth-brownie-1.17.2, xdist-1.34.0, hypothesis-6.27.3, web3-5.25.0, forked-1.3.0
collected 0 items

=========================================================== warnings summary ============================================================
../../../../.local/pipx/venvs/eth-brownie/lib/python3.8/site-packages/brownie/network/main.py:44
  /Users/liwei/.local/pipx/venvs/eth-brownie/lib/python3.8/site-packages/brownie/network/main.py:44: BrownieEnvironmentWarning: Development network has a block height of 2
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================================================== 1 warning in 0.49s ===========================================================

推荐答案

问题已解决,忘记编写";def TEST_Deploy():";

def test_deploy():
    # Arrange
    account = accounts[0]

    # Act
    simple_storage = SimpleStorage.deploy({"from": account})
    starting_value = simple_storage.retrieve()
    expected = 0
    # Assert

    assert starting_value == expected

这篇关于Brownie测试索引错误:列表索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:34