Testing a Contract

This documentation recommends the use of the pytest framework with the ethereum-tester package. 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.

주석

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.

Vyper Contract and Basic Fixtures

This is the base requirement to load a vyper contract and start testing. The last 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.

Load Contract and Basic Tests

Assume the following simple contract storage.vy. It has a single integer variable and a function to set that value.

We create a test file test_storage.py where we write our tests in pytest style.

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.

주석

To run the tests, call pytest or python -m pytest from your project directory.

Events and Failed Transactions

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

Next, we take a look at the two fixtures that will allow us to read the event logs and to check for failed transactions.

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.

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.