pytest-wake

0.4.3 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic `pytest` test for a Solidity smart contract using the `Wake` framework. First, create a `contracts/MyContract.sol` file with a simple Solidity contract. Then, initialize a Wake project and generate Python `pytypes` by running `wake init` and `wake pytypes -w` in your project's root directory. These commands set up the project structure and create Python classes (pytypes) that represent your Solidity contracts, enabling type-safe interaction. Finally, write a `pytest` test file (e.g., `test_my_contract.py`) that imports and uses these generated pytypes to interact with and assert properties of your deployed contract.

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.")

view raw JSON →