Introduction to Populus Testing Part of Blockchain Testing
Posted By : Sonali Gupta | 10-May-2018
Introduction
The Populus framework provides some python code for testing contracts. Testing in the ethereum blockchain by using the python testing framework “py.test”.
By default tests and run the code of ethereum blockchain which is already saved in computer memory.
The drive for tests get placed in the./tests/ directory in the root of the project. In order for py.test to find your, tests modules(File name) their module name must start with test_.
Test Contracts
For testing the contracts we are specially using populus testing. These contract filenames should match or saved with the name as “Test*.sol” and be located anywhere in the memory of project tests directory ./tests/.
Running Tests With Pytest
To run the full test suite of your project:
$
py.test tests/
Or to run a specific test
$
py.test tests/test_greeter.py
Pytest Fixtures
The fixtures provided by populus for making testing easy. In order to use a fixture in tests to add an argument with the same name to the signature in the test function.
Project:- Firstly project object is initialized, and then the fixtures are derived from it.
def
test_project_things(project):
#
directory things
assert
project.project_dir
==
'/path/to/my/project'
#
raw compiled contract access
assert
'MyContract'
in
project.compiled_contract_data
Chain:- A running 'tester' test chain.
def
test_greeter(chain):
greeter,
_ =
chain.provider.get_or_deploy_contract('Greeter')
assert
greeter.call().greet()
==
"Hello"
def
test_deploying_greeter(chain):
GreeterFactory
=
chain.provider.get_contract_factory('Greeter')
deploy_txn_hash
=
GreeterFactory.deploy()
...
Registrar:-Convenience fixture for the "chain.registrar" property.
Provider:- Convenience fixture for the"chain.provider"property.
Web3:- Web3.py instance configure to connect to with the chain fixture.
def
test_account_balance(web3, chain):
initial_balance
=
web3.eth.getBalance(web3.eth.coinbase)
wallet
=
chain.get_contract('Wallet')
withdraw_txn_hash
=
wallet.transact().withdraw(12345)
withdraw_txn_receipt
=
chain.wait.for_receipt(withdraw_txn_hash)
after_balance
=
web3.eth.getBalance(web3.eth.coinbase)
assert
after_balance -
initial_balance ==
1234
Base Contract Factories:- The contract factory classes for the project. Will be associated with the Web3 instance through the web3 fixture.
def
test_wallet_deployment(web3,
base_contract_factories):
WalletFactory
=
base_contract_factories.Wallet
deploy_txn_hash
=
WalletFactory.deploy()
Accounts:- The web3.eth.accounts property off, related with the web3 fixture
def
test_accounts(web3, accounts):
assert
web3.eth.coinbase
==
accounts[0]
One case for the contract which needs to give constructor as an argument. Now provide fixtures for a token contract which requires a constructor argument to set the initial supply.
import
pytest
@pytest.fixture()
def
token_contract(chain):
TokenFactory
=
chain.get_contract_factory('Token')
deploy_txn_hash
=
TokenFactory.deploy(arguments=[
1e18,
#
initial token supply
)
contract_address
=
chain.wait.for_contract_address(deploy_txn_hash)
return
TokenFactory(address=contract_address)
Now, use this fixture in the tests, same way use the built-in populus fixtures.
def test_initial_supply(token_contract):
assert token_contract.call().totalSupply() == 1e18
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sonali Gupta
Sonali is certified in manual testing and selenium web driver. She is a B.Tech through Electronics and Communication.