Struct libtest_mimic::Trial

source ·
pub struct Trial { /* private fields */ }
Expand description

A single test or benchmark.

The original libtest often calls benchmarks “tests”, which is a bit confusing. So in this library, it is called “trial”.

A trial is created via Trial::test or Trial::bench. The trial’s name is printed and used for filtering. The runner is called when the test/benchmark is executed to determine its outcome. If runner panics, the trial is considered “failed”. If you need the behavior of #[should_panic] you need to catch the panic yourself. You likely want to compare the panic payload to an expected value anyway.

Implementations§

source§

impl Trial

source

pub fn test<R>(name: impl Into<String>, runner: R) -> Self
where R: FnOnce() -> Result<(), Failed> + Send + 'static,

Creates a (non-benchmark) test with the given name and runner.

The runner returning Ok(()) is interpreted as the test passing. If the runner returns Err(_), the test is considered failed.

source

pub fn bench<R>(name: impl Into<String>, runner: R) -> Self
where R: FnOnce(bool) -> Result<Option<Measurement>, Failed> + Send + 'static,

Creates a benchmark with the given name and runner.

If the runner’s parameter test_mode is true, the runner function should run all code just once, without measuring, just to make sure it does not panic. If the parameter is false, it should perform the actual benchmark. If test_mode is true you may return Ok(None), but if it’s false, you have to return a Measurement, or else the benchmark is considered a failure.

test_mode is true if neither --bench nor --test are set, and false when --bench is set. If --test is set, benchmarks are not ran at all, and both flags cannot be set at the same time.

source

pub fn with_kind(self, kind: impl Into<String>) -> Self

Sets the “kind” of this test/benchmark. If this string is not empty, it is printed in brackets before the test name (e.g. test [my-kind] test_name). (Default: empty)

This is the only extension to the original libtest.

source

pub fn with_ignored_flag(self, is_ignored: bool) -> Self

Sets whether or not this test is considered “ignored”. (Default: false)

With the built-in test suite, you can annotate #[ignore] on tests to not execute them by default (for example because they take a long time or require a special environment). If the --ignored flag is set, ignored tests are executed, too.

source

pub fn name(&self) -> &str

Returns the name of this trial.

source

pub fn kind(&self) -> &str

Returns the kind of this trial. If you have not set a kind, this is an empty string.

source

pub fn has_ignored_flag(&self) -> bool

Returns whether this trial has been marked as ignored.

source

pub fn is_test(&self) -> bool

Returns true iff this trial is a test (as opposed to a benchmark).

source

pub fn is_bench(&self) -> bool

Returns true iff this trial is a benchmark (as opposed to a test).

Trait Implementations§

source§

impl Debug for Trial

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Trial

§

impl !RefUnwindSafe for Trial

§

impl Send for Trial

§

impl !Sync for Trial

§

impl Unpin for Trial

§

impl !UnwindSafe for Trial

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.