Skip to main content

check

Attribute Macro check 

Source
#[check]
Expand description

Turns a function into a property test.

It takes a list of generators as input, which are used to produce random arguments for the function it’s attached to.

The function can return (), bool, or Result to indicate whether the property holds. Any panic within the function is also treated as a test failure.

§Arguments

  • Generators: The first arguments to the macro are a comma-separated list of generators. The values produced by these generators will be passed as arguments to the test function. You can use _ to infer the default generator for a type.
  • verbose: A boolean (true or false) to enable or disable verbose output, which shows every generation and shrink step. Defaults to false.
  • color: A boolean (true or false) to enable or disable colored output. Defaults to true.
  • debug: A boolean (true or false) that controls the output format. If true, the full Debug representation of test results is printed. If false, a more minimal output is used. Defaults to true.

§Examples

A simple test with a range generator:

#[check(0..100)]
fn is_less_than_100(x: i32) {
    assert!(x < 100);
}

Using multiple generators and inferring a type:

#[check(.., 0.0..1.0, _)]
fn complex_test(x: i32, y: f64, z: bool) {
    // ...
}

Disabling color and enabling verbose output:

#[check(0..10, color = false, verbose = true)]
#[should_panic]
fn failing_test(x: i32) {
    assert!(x > 5);
}

An in-place replacement for the #[test] attribute that allows adding parameters to test functions and providing Generate expressions as arguments to this attribute. See examples::cheats. for usage examples.