Ethereum Tester is a tool suite for testing Ethereum based applications.
This section provides a quick overview of testing with eth-tester
. To learn more, you can view the documentation at the Github repo or join the Gitter channel.
Prior to testing, the Vyper specific contract conversion and the blockchain related fixtures need to be set up. These fixtures will be used in every test file and should therefore be defined in conftest.py.
Note
Since the testing is done in the pytest framework, you can make use of pytest.ini, tox.ini and setup.cfg and you can use most IDEs' pytest plugins.
.. literalinclude:: ../tests/base_conftest.py :language: python :linenos:
The final two fixtures are optional and will be discussed later. The rest of this chapter assumes that you have this code set up in your conftest.py
file.
Alternatively, you can import the fixtures to conftest.py
or use pytest plugins.
Assume the following simple contract storage.vy
. It has a single integer variable and a function to set that value.
.. literalinclude:: ../examples/storage/storage.vy :linenos: :language: python
We create a test file test_storage.py
where we write our tests in pytest style.
.. literalinclude:: ../tests/examples/storage/test_storage.py :linenos: :language: python
First we create a fixture for the contract which will compile our contract and set up a Web3 contract object. We then use this fixture for our test functions to interact with the contract.
Note
To run the tests, call pytest
or python -m pytest
from your project directory.
To test events and failed transactions we expand our simple storage contract to include an event and two conditions for a failed transaction: advanced_storage.vy
.. literalinclude:: ../examples/storage/advanced_storage.vy :linenos: :language: python
Next, we take a look at the two fixtures that will allow us to read the event logs and to check for failed transactions.
.. literalinclude:: ../tests/base_conftest.py :language: python :pyobject: assert_tx_failed
The fixture to assert failed transactions defaults to check for a TransactionFailed
exception, but can be used to check for different exceptions too, as shown below. Also note that the chain gets reverted to the state before the failed transaction.
.. literalinclude:: ../tests/base_conftest.py :language: python :pyobject: get_logs
This fixture will return a tuple with all the logs for a certain event and transaction. The length of the tuple equals the number of events (of the specified type) logged and should be checked first.
Finally, we create a new file test_advanced_storage.py
where we use the new fixtures to test failed transactions and events.
.. literalinclude:: ../tests/examples/storage/test_advanced_storage.py :linenos: :language: python