Skip to main content

Pytest support for cairo-lang and starknet

Project description

pytest-cairo: pytest support for cairo-lang and starknet

Usage

To install:

$ pip install pytest-cairo

The plugin will automatically run any function with a test prefix, from files with a test_ prefix and a .cairo extension.

Examples

Basic tests

Consider the following Starknet contract:

# Contents of contract.cairo
%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin

func calculate_inverse{
    syscall_ptr : felt*,
    pedersen_ptr : HashBuiltin*,
    range_check_ptr,
}(val : felt) -> (res : felt):
    return (1 / val)
end

We could write a basic test for the function calculate_inverse:

# Contents of test_contract.cairo
%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin

from contracts.contract import calculate_inverse

@external
func test_calculate_inverse{
    syscall_ptr : felt*,
    pedersen_ptr : HashBuiltin*,
    range_check_ptr,
}() -> ():
    let (actual) = calculate_inverse(2)
    let expected = 1 / 2
    assert actual = expected
    return ()
end

Additionally we could use a raises attribute to assert that the function fails if we pass val=0:

@external
func test_calculate_inverse_expected_exception{
    syscall_ptr : felt*,
    pedersen_ptr : HashBuiltin*,
    range_check_ptr,
}() -> ():
    with_attr raises("assert_not_zero failed"):
        calculate_inverse(0)
    end
    return ()
end

Deploying contracts

Contracts can be deployed from tests using the deploy_contract helper function. Consider the same contract from the previous example, but extended with a constructor:

# Contents of contracts/contract.cairo
%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin

@constructor
func constructor{
    syscall_ptr : felt*,
    pedersen_ptr : HashBuiltin*,
}(arg : felt):
    return ()
end

func calculate_inverse{
    syscall_ptr : felt*,
    pedersen_ptr : HashBuiltin*,
    range_check_ptr,
}(val : felt) -> (res : felt):
    return (1 / val)
end

We can deploy this contract in our test_contract function and call its methods:

%lang starknet

from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.cairo_builtins import HashBuiltin

from contracts.interfaces.IContract import IContract

from pytest_cairo.contract_index import contracts
from pytest_cairo.helpers import deploy_contract

@view
func test_contract{
    syscall_ptr : felt*,
    pedersen_ptr : HashBuiltin*,
    range_check_ptr,
}() -> ():

    let (calldata : felt*) = alloc()
    assert calldata[0] = 1  # Value is not relevant
    let (contract_address) = deploy_contract(contracts.contract, 1, calldata)

    let (result) = IContract.calculate_inverse(
        contract_address=contract_address, val=4)

    assert result = 1/4

    return ()
end

The contract_index provides a reference to contracts and is generated automatically by indexing folders that contain .cairo files in the current working directory. In the above example the contract contract.cairo is located in a folder called contracts, so we can reference it as contracts.contract.

Other helper functions

Other available helper functions:

  • set_block_number: sets the current block number
  • set_block_timestamp: sets the current block timestamp
  • set_caller_address: sets the caller address returned by get_caller_address
  • set_contract_address: sets the contract address of the current test contract. This is useful when calling other contracts, since this address will be returned when the other contract calls get_caller_address
  • impersonate: sets both the caller address and the contract address. The former is useful when testing functions directly, the latter when calling functions in other contracts

Helper functions can be imported from pytest_cairo.helpers.

Development

To install development dependencies, run:

$ pip install -r requirements-dev.txt

Run tests with:

$ pytest

or:

$ docker-compose run test

To run tests against all supported interpreters (using docker-compose):

$ tox

This assumes tox, docker and docker-compose are installed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-cairo-0.0.4.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

pytest_cairo-0.0.4-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-cairo-0.0.4.tar.gz.

File metadata

  • Download URL: pytest-cairo-0.0.4.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pytest-cairo-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f1932e819879a346df01681c076edfdbfca440b1ff88a9f2a105e03582a4e617
MD5 7e768e9422ed18cd11c9879ac44df15f
BLAKE2b-256 c748572fdff9a96af351e299fbef36d8559aa20a4ea53d5eb2e4ac2ba484b7c2

See more details on using hashes here.

File details

Details for the file pytest_cairo-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pytest_cairo-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pytest_cairo-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ed0807c32accb560aef1256323b17e1d3c9128fc14b834eba5a36010804e2b04
MD5 71f9bbc9ab6fa8c6fdf5c9b16b1a7928
BLAKE2b-256 0a4e4bd5f8ed4f07d0682710feeaadd388eaf7a0d7c2b53ceb2a069312de4ad0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page