IndexError raised when passing an element of brownie.accounts as the exclude argument to brownie.test.strategy
See original GitHub issueEnvironment information
brownie
Version: 1.12.3, 1.12.2, 1.12.1ganache-cli
Version: 6.12.1solc
Version: 0.8.0+commit.c7dfd78e.Linux.g++- Python Version: 3.8.3, 3.9.1
- OS: linux
What was wrong?
Command: brownie test
Code that caused failure:
from brownie import accounts
from brownie.test import given, strategy
@given(
amount=strategy("uint256", max_value="1000 ether"),
to=strategy("address", exclude=accounts[0]),
)
def test_transfer_success_returns_true(token, accounts, amount, to):
assert token.transfer(to, amount, {"from": accounts[0]}) == True
Error output:
$ brownie test tests/test_transfer.py
Brownie v1.12.3 - Python development framework for Ethereum
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.8.3, pytest-6.0.1, py-1.10.0, pluggy-0.13.1
rootdir: /home/skelletor/projects/CloudToken
plugins: eth-brownie-1.12.3, hypothesis-5.41.3, forked-1.3.0, web3-5.11.1, xdist-1.34.0
collected 0 items / 1 error
===================================================================================================== ERRORS ======================================================================================================
_____________________________________________________________________________________ ERROR collecting tests/test_transfer.py _____________________________________________________________________________________
tests/test_transfer.py:8: in <module>
to=strategy("address", exclude=accounts[0]),
E IndexError: list index out of range
============================================================================================= short test summary info =============================================================================================
FAILED tests/test_transfer.py - IndexError: list index out of range
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================================ 1 error in 0.15s =================================================================================================
Extra Information
- I was following @iamdefinitelyahuman’s In-Depth Guide to Testing Ethereum Smart Contracts Part Six, which was published on July 23, 2020. Since the test in the guide and mine are essentially the same, and assuming that the code in the guide worked when the content was published, then the bug was introduced between July 23, 2020 and now.
- I’ve successfully recreated the error above with the v1.12.x releases.
- Using slice notation (
accounts[:1]
), the IndexError doesn’t get raised, however, the elementaccounts[0]
is included in the sampling. This makes sense, because a slice of an empty list, will return an empty list, which is essentially excluding nothing.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Property-Based Testing — Brownie 1.19.2 documentation
In the following example, we add a to argument using an address strategy. from brownie import accounts from brownie.test import given ...
Read more >eth-brownie/community - Gitter
what I ment to ask, how do I define accounts at the start for all fixtures? deployer = accounts[0] E IndexError: list index...
Read more >Brownie test IndexError: list index out of range
When connecting to a remote network via a hosted node such as Infura, the Accounts container will be empty. Before you can perform...
Read more >Brownie Documentation
Brownie is a Python framework for Ethereum smart contract testing, ... an argument with the same name to the inputs of your test...
Read more >Programming Style - GitHub Pages
PEP8: a style guide for Python that discusses topics such as how you should name variables, how you should use indentation in your...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Took a look at this… As you said, the problem is that test collection happens prior to connecting to the network, so
accounts
is empty.Connecting to the network is handled in
pytest_collection_finish
, inbrownie/test/runner.py
, line 258. This is a deliberate design choice for 2 reasons:ganache-cli
to launch and be killed.There is no easy solution I can see here unfortunately, as the call to
strategy
happens immediatly upon importing the test module. A hacky solution might be to allowAccounts
to return a sort-of proxy “promise” object that represents the eventualAccount
that will exist at index0
once the network has connect. The first attempt to inspect this object would then mutate it into the actual account object, or raise if the index is out of bounds.I was hoping to find a solution which would wrap this for the user so that they don’t have to worry about it, however it seems that it’s functioning as designed and so this won’t be feasible? If this is the case, alternatively could we just modify the documentation for the
address
strategy in order to notate that it requires loading the accounts object first?