cairo by example

assert

To make sure our tests work, we use assert.

fn main(x: felt252, y: felt252) {
    assert(x != y, 'error, x is equal to y');
}

#[test]
fn test_main() {
    main(1,2);
}

The first argument of assert is the condition we want to check, and the second is a message we will see on the console if the condition is false.

Run cairo-test file_name

Try changing it so that the test fails.



Try it out!
  1. Install the toolchain:
    • For macOS and Linux, run our script:
    • curl -sL https://raw.githubusercontent.com/lambdaclass/cairo-by-example/main/build/installer.sh | bash -s 2.2.0
    • For Windows and others, please see the official guide
  2. Run the example:
    1. Copy the example into a assert.cairo file and run with:
    2. %!s(<nil>) assert.cairo

prev (hello world) next (variables)