This session aims to teach you how to start testing your contract in Cairo.
Slides: TBA
Lecture Video: TBA
To install Scarb, follow the installation process from here.
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
We will continue to work from the previous project. You can find the project source here.
Another way to initialize your project is by executing the following command:
snforge --init PROJECT_NAME
More about the project structure here.
Cheatcodes is a powerful feature withn the Starknet Foundry tool. These cheatcodes offer multiple advantages when testing a project. These are the following:
- Changes the caller address for a contract at the given address.
stop_prank()
to cancel it
- Mocks contract call to a function_name of a contract at the given address
stop_mock_call()
to cancel it
- Changes the block number for a contract at the given address.
stop_roll()
to cancel it
- 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.
declare()
- declares a contract and returns theContractClass
precalculate_address()
- calculate an address of a contract in advance prior deployingdeploy()
- deploys a contract and returns its addressread_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.
- 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.
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();
Allows running an entire test suite against a specific forked environment.
More information, here.
To track current progress, check here.