Brownie test IndexError: list index out of range
Macbook Pro : Monterey
Intel Core i7
Brownie v1.17.2
Ganache CLI v6.12.2 (ganache-core: 2.13.2)
I am learning solidity according to reference(https://www.youtube.com/watch?v=M576WGiDBdQ&t=25510s).
I wrote and deployed a smart contract(scripts/deploy.py) using brownie framework, it worked.
Then tried to write a test script(tests/test_simple_storage.py) that gave me error information in the terminal.
Googled this and tried to delete all the files in brownie projects' build folder and deployed the contract(scripts/deploy.py) again, it's the same error.
And tried to change the private key and index, for accounts[0]to account[-1]and account[1], the same error result.
FYI:I typed the command "ganache-cli —deterministic " in the terminal. So the account and private key are not random.
Saved the Ganache's account[0]'s private key in .env files,like this
.env
export PRIVATE_KEY=0x91114a07f248a1c50951cb11557af5424cc6a49bf61521874c9ae3f4ae239a6d
Error Info:
(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 %
test 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
smartcontract deployed well,use the same local blockchain,ganache-cli “scripts/deploy.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()
updated on 20220111
After read brownie docs find this command "network.connect("development")" ,is this command set the network right , I remembered brownie should default connect to development network.
so updated the code like this
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
and the terminal give one warning information
(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 ===========================================================
Solution 1:
When connecting to a remote network via a hosted node such as Infura, the Accounts container will be empty. Before you can perform any transactions you must add a local account to Brownie. Looks like you are not on development environment.
Brownie documentation will guide you how to set up accounts