Skip to content

#[tanu::test] Attribute

The #[tanu::test] attribute is used to mark functions as test functions in the Tanu framework. When a function is annotated with this attribute, it will be executed as part of the test suite.

Usage

To use the #[tanu::test] attribute, simply add it above the function definition:

use tanu::check_eq;

#[tanu::test]
async fn my_test_function() -> eyre::Result<()> {
    let result = 2 + 2;
    check_eq!(result, 4);
    Ok(())
}

Notes * The #[tanu::test] attribute can only be applied to "async" functions. * Functions marked with #[tanu::test] should not take any arguments and should not return any values. * The tanu framework will automatically discover and run all functions marked with the #[tanu::test] attribute when the test suite is executed.

Parameterized Tests

#[tanu::test] attribute is inspired by test_case crate where you can easily parameterize test case by providing parameters in the attribute body as follows.

use tanu::check_eq;

#[tanu::test(10, 10, 20)]
#[tanu::test(20, 20, 40)]
async fn my_test_function(a: u32, b: u32, expected: u32) -> eyre::Result<()> {
    let result = a + b;
    check_eq!(result, expected);
    Ok(())
}

If you run a parameterized test, the test name will be automatically generated by concatenating the stringified parameters, resulting in my_test_function_10_10_20.

Sometimes parameters can't be strigified, in such case, tanu refuse to compile the code. In such cases, tanu refuses to compile the code. If that happens, you can specify your desired name rather than the auto-generated one. Note that a test name is delimited by ; from the parameters.

use tanu::check_eq;

#[tanu::test(10, 10, 20; "add_10_and_10_equal_20")]
#[tanu::test(20, 20, 40; "add_20_and_20_equal_40")]
async fn my_test_function(a: u32, b: u32, expected: u32) -> eyre::Result<()> {
    let result = a + b;
    check_eq!(result, expected);
    Ok(())
}