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.

Example

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

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.3.tar.gz (11.6 kB view hashes)

Uploaded Source

Built Distribution

pytest_cairo-0.0.3-py3-none-any.whl (8.4 kB view hashes)

Uploaded Python 3

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