Skip to content

robertkodra/starknet-basecamp-7-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Basecamp 07 - Session 4: Testing

This session aims to teach you how to start testing your contract in Cairo.

Slides: TBA

Lecture Video: TBA

Nicolas Cage Let's Ride

Getting Started

Scarb:

To install Scarb, follow the installation process from here.

Starknet Foundry:

To install Starknet Foundry, follow the installation process from here.

curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh

Then check if the installation has been successful by running:

snforge --version
>> forge 0.6.0

Setting up the project

We will continue to work from the previous project. You can find the project source here.

Initializing your project with snforge (optional)

Another way to initialize your project is by executing the following command:

snforge --init PROJECT_NAME

More about the project structure here.

Cheatcodes

Cheatcodes is a powerful feature withn the Starknet Foundry tool. These cheatcodes offer multiple advantages when testing a project. These are the following:

start_prank()

  • Changes the caller address for a contract at the given address.
  • stop_prank() to cancel it

start_mock_call()

  • Mocks contract call to a function_name of a contract at the given address
  • stop_mock_call() to cancel it

start_roll()

  • Changes the block number for a contract at the given address.
  • stop_roll() to cancel it

start_warp()

  • Changes the block timestamp for a contract at the given address.
  • stop_warp() to cancel it

For the full list of available cheatcodes, check out the Cheatcodes Reference page.

Forge Library Functions

  • declare() - declares a contract and returns the ContractClass
  • precalculate_address() - calculate an address of a contract in advance prior deploying
  • deploy() - deploys a contract and returns its address
  • read_txt() - read and parse text file content to an array of felts (read_json())
  • print() - print the test data

For the full list of the Library Functions Referneces click here.

Upcoming releases:

Fuzz testing

  • Currently only felt252 is supported, however other data types will be supported in the upcoming version.

More informatio nabout fuzz testing, here.

snforge --fuzzer-runs 1000 --fuzzer-seed 42

A more configurable approach with #661:

#[test]
#[fuzzer ....]
fn test_fuzzer(){

}

To track current progress, check here.

Gas Report gas

For now, the gas report will be released once Cario v2.3.0 is available.

As an alternative, you can perform the following to find out the gas usage:

    let initial_gas = testing::get_available_gas();
    gas::withdraw_gas().unwrap();

    // CODE HERE

    (initial_gas - testing::get_available_gas()).print();

State Forking

Allows running an entire test suite against a specific forked environment.

More information, here.

To track current progress, check here.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published