pytest-wake
pytest-wake is a pytest plugin that integrates the Wake Solidity development and fuzz testing framework with the pytest testing framework. It enables writing tests for Solidity smart contracts using Python, leveraging pytest's capabilities and Wake's `pytypes` for type-safe interactions. The current version is 0.4.3, released on November 19, 2024. While specific release cadence details for `pytest-wake` are not explicitly stated, its development is tied to the active Wake framework.
Warnings
- gotcha When Wake generates Python 'pytypes' from Solidity contracts, name collisions can occur if a Solidity type name is a Python keyword or reserved name. In such cases, Wake appends an underscore to the generated Python type name (e.g., `class` becomes `class_`). Overloaded Solidity functions also result in appended underscores (e.g., `foo` and `foo_`).
- gotcha pytest's test discovery mechanism relies on specific file and function naming conventions (e.g., `test_*.py` or `*_test.py` for files, and `test_*` for functions/methods). Incorrect naming can lead to tests not being found or executed by pytest, even if `pytest-wake` is installed.
- breaking As a pytest plugin, `pytest-wake`'s functionality is dependent on the installed `pytest` version. Major `pytest` releases (e.g., `pytest 8.0`, `pytest 9.0`) often introduce breaking changes to their API or internal mechanisms that could affect plugins. If `pytest-wake` is not updated to support a new major `pytest` version, it may lead to unexpected behavior or failures.
Install
-
pip install pytest-wake eth-wake
Imports
- pytest
import pytest
- GeneratedContractPytype
from pytypes.contracts.MyContract import MyContract
Quickstart
import pytest
import os
# Assuming a file `contracts/MyContract.sol` exists with a simple contract:
# pragma solidity ^0.8.0;
# contract MyContract {
# uint public value;
# constructor(uint _value) { value = _value; }
# function setValue(uint _newValue) public { value = _newValue; }
# }
# You would typically run 'wake init' and 'wake pytypes' in your project root
# to generate the 'pytypes' directory before running tests.
# For example: wake init --force; wake pytypes -w
# These imports would be generated by 'wake pytypes'
# For this example, we'll assume a basic contract structure and manual pytype creation
# In a real scenario, Wake's CLI would manage this.
# To make this runnable without `wake pytypes` output, we will mock the contract interaction slightly.
try:
from pytypes.contracts.MyContract import MyContract
# In a real setup, you'd also need a 'default_chain' fixture provided by Wake
# For demonstration, we'll simulate contract deployment and interaction.
class MockContract:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
def setValue(self, new_value):
self._value = new_value
return self # Simulate fluent interface
# This fixture would typically be provided by Wake
@pytest.fixture
def deployed_my_contract():
# In a real Wake test, this would deploy your contract to a chain
# and return the pytype instance connected to it.
return MockContract(10)
def test_my_contract_initial_value(deployed_my_contract):
assert deployed_my_contract.value == 10
def test_my_contract_set_value(deployed_my_contract):
deployed_my_contract.setValue(20)
assert deployed_my_contract.value == 20
except ImportError:
print("Skipping contract tests: 'pytypes' not generated or Wake environment not set up.")
print("Please run 'wake init' and 'wake pytypes -w' in your project directory.")
def test_placeholder_contract():
pytest.skip("Contract pytypes not available, Wake environment not set up.")