Attribute Macro googletest::test

source ·
#[test]
Expand description

Marks a test to be run by the Google Rust test runner.

Annotate tests the same way ordinary Rust tests are annotated:

#[googletest::test]
fn should_work() {
    ...
}

The test function is not required to have a return type. If it does have a return type, that type must be googletest::Result. One may do this if one wishes to use both fatal and non-fatal assertions in the same test. For example:

#[googletest::test]
fn should_work() -> googletest::Result<()> {
    let value = 2;
    expect_that!(value, gt(0));
    verify_that!(value, eq(2))
}

This macro can be used with #[should_panic] to indicate that the test is expected to panic. For example:

#[googletest::test]
#[should_panic]
fn passes_due_to_should_panic() {
    let value = 2;
    expect_that!(value, gt(0));
    panic!("This panics");
}

Using #[should_panic] modifies the behaviour of #[googletest::test] so that the test panics (and passes) if any non-fatal assertion occurs. For example, the following test passes:

#[googletest::test]
#[should_panic]
fn passes_due_to_should_panic_and_failing_assertion() {
    let value = 2;
    expect_that!(value, eq(0));
}