Test

Sway provides the #[test] attribute which enables unit tests to be written in Sway.

Success case

The #[test] attribute indicates that a test has passed if it did not revert.

#[test]
fn equal() {
    assert_eq(1 + 1, 2);
}

Revert Case

To test a case where code should revert we can use the #[test(should_revert)] annotation. If the test reverts then it will be reported as a passing test.

#[test(should_revert)]
fn unequal() {
    assert_eq(1 + 1, 3);
}

We may specify a code to specifically test against.

#[test(should_revert = "18446744073709486084")]
fn assert_revert_code() {
    assert(1 + 1 == 3);
}

#[test(should_revert = "42")]
fn custom_revert_code() {
    revert(42);
}