Skip to main content

TestContext

Struct TestContext 

Source
pub struct TestContext { /* private fields */ }

Implementations§

Source§

impl TestContext

Source

pub fn new() -> Self

Source

pub fn stdout<S>(&mut self, value: S)
where S: AsRef<[u8]>,

Examples found in repository?
examples/example_smoke.rs (line 9)
5fn main() {
6    let tests = collect_tests(|| {
7        describe!("math", {
8            test!("adds two numbers", |ctx| {
9                ctx.stdout("ok\n");
10                expect(2 + 3).to_be(5);
11            });
12
13            test!("handles negatives", id = "math/handles-negatives", |ctx| {
14                assert!(ctx.observe().metric("wall_time_ns", 42.0));
15                expect(-2 + 1).to_be(-1);
16            });
17        });
18    })
19    .expect("collection should validate");
20
21    for test in &tests {
22        let outcome = run_test(test);
23        println!("{} => {}", test.canonical_name(), outcome.exit);
24    }
25}
More examples
Hide additional examples
examples/host_example.rs (line 9)
5fn main() {
6    let tests = collect_tests(|| {
7        describe!("pkg", {
8            test!("smoke test", id = "pkg::smoke", |ctx| {
9                ctx.stdout("ok\n");
10                expect(true).to_be_truthy();
11            });
12
13            test!("failing test", id = "pkg::fail", |ctx| {
14                ctx.stderr("failure\n");
15                ctx.set_exit(1);
16            });
17        });
18    })
19    .expect("collection should validate");
20
21    let exit_code = match observer_host_main("rust", &tests) {
22        Ok(()) => 0,
23        Err(error) => {
24            eprintln!("{error}");
25            2
26        }
27    };
28    std::process::exit(exit_code);
29}
examples/host_embed_example.rs (line 13)
9fn main() {
10    let tests = collect_tests(|| {
11        describe!("pkg", {
12            test!("embedded smoke test", id = "pkg::embedded-smoke", |ctx| {
13                ctx.stdout("ok\n");
14                expect(true).to_be_truthy();
15            });
16        });
17    })
18    .expect("collection should validate");
19
20    let args = std::env::args().collect::<Vec<_>>();
21    if args.get(1).map(String::as_str) == Some("observe") {
22        let exit_code = match observer_host_dispatch_embedded("rust", "observe", &tests, args) {
23            Ok(()) => 0,
24            Err(error) => {
25                eprintln!("{error}");
26                2
27            }
28        };
29        std::process::exit(exit_code);
30    }
31
32    app_main(&args);
33}
Source

pub fn stderr<S>(&mut self, value: S)
where S: AsRef<[u8]>,

Examples found in repository?
examples/host_example.rs (line 14)
5fn main() {
6    let tests = collect_tests(|| {
7        describe!("pkg", {
8            test!("smoke test", id = "pkg::smoke", |ctx| {
9                ctx.stdout("ok\n");
10                expect(true).to_be_truthy();
11            });
12
13            test!("failing test", id = "pkg::fail", |ctx| {
14                ctx.stderr("failure\n");
15                ctx.set_exit(1);
16            });
17        });
18    })
19    .expect("collection should validate");
20
21    let exit_code = match observer_host_main("rust", &tests) {
22        Ok(()) => 0,
23        Err(error) => {
24            eprintln!("{error}");
25            2
26        }
27    };
28    std::process::exit(exit_code);
29}
Source

pub fn set_exit(&mut self, exit: i32)

Examples found in repository?
examples/host_example.rs (line 15)
5fn main() {
6    let tests = collect_tests(|| {
7        describe!("pkg", {
8            test!("smoke test", id = "pkg::smoke", |ctx| {
9                ctx.stdout("ok\n");
10                expect(true).to_be_truthy();
11            });
12
13            test!("failing test", id = "pkg::fail", |ctx| {
14                ctx.stderr("failure\n");
15                ctx.set_exit(1);
16            });
17        });
18    })
19    .expect("collection should validate");
20
21    let exit_code = match observer_host_main("rust", &tests) {
22        Ok(()) => 0,
23        Err(error) => {
24            eprintln!("{error}");
25            2
26        }
27    };
28    std::process::exit(exit_code);
29}
Source

pub fn fail(&mut self, message: &str)

Source

pub fn observe(&mut self) -> Observe<'_>

Examples found in repository?
examples/example_smoke.rs (line 14)
5fn main() {
6    let tests = collect_tests(|| {
7        describe!("math", {
8            test!("adds two numbers", |ctx| {
9                ctx.stdout("ok\n");
10                expect(2 + 3).to_be(5);
11            });
12
13            test!("handles negatives", id = "math/handles-negatives", |ctx| {
14                assert!(ctx.observe().metric("wall_time_ns", 42.0));
15                expect(-2 + 1).to_be(-1);
16            });
17        });
18    })
19    .expect("collection should validate");
20
21    for test in &tests {
22        let outcome = run_test(test);
23        println!("{} => {}", test.canonical_name(), outcome.exit);
24    }
25}
Source

pub fn as_raw_mut(&mut self) -> &mut TestContext

Source

pub fn finish(self) -> TestOutcome

Trait Implementations§

Source§

impl Default for TestContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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>,

Source§

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>,

Source§

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.