noir by example

assert

to ensure some property holds true, use assert(...)

fn main(x : Field, y : pub Field) {
    assert(x != y);
}

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

to test a given input fails, use the #[test(should_fail)] decorator

#[test(should_fail)]
fn test_main_fails() {
    main(1, 1);
}

often there’s multiple assertions that could’ve failed. adding a message helps to target a specific statement in your tests

fn main(x : Field, y : pub Field) {
    assert(x != y);

    // optionally include an error message
    assert(x + y == 3, "Must sum to three");
}

#[test(should_fail_with = "Must sum to three")]
fn test_main_fails_with_message() {
    main(3, 4);
}

try it out install the noir toolchain if you haven't yet:
        curl -L https://noirup.org/install | bash
        noirup --nightly
        
then checkout and run the example:
        git clone https://github.com/sambarnes/noir-by-example.git
        cd noir-by-example/circuits/examples/assert
        nargo test --show-output
        

prev (hello world) next (variables)