6 releases (stable)

2.0.0 Apr 26, 2021
1.3.0 Sep 19, 2020
0.1.0 Sep 14, 2020

#412 in Testing

Download history 48/week @ 2024-07-22 110/week @ 2024-07-29 27/week @ 2024-08-05 31/week @ 2024-08-12 23/week @ 2024-08-19 72/week @ 2024-08-26 18/week @ 2024-09-02 31/week @ 2024-09-09 20/week @ 2024-09-16 70/week @ 2024-09-23 30/week @ 2024-09-30 26/week @ 2024-10-07 31/week @ 2024-10-14 21/week @ 2024-10-21 19/week @ 2024-10-28 27/week @ 2024-11-04

98 downloads per month
Used in 8 crates

MIT license

48KB
1.5K SLoC

laboratory

A simple, expressive unit test framework for Rust

GitHub Workflow Status (branch) Crates.io Crates.io

Checkout the documentation and the extensive examples over on github.

Laboratory is layer 2 test runner solution that sits on top of the Rust test runner to provide unparalleled features and ease of use.

Features

  • before_all, before_each, after_all, after_each hooks
  • Different reporter options: spec, minimal, json, json-pretty, rust, dot, tap, list
  • Reports test durations in: nanoseconds, microseconds, milliseconds and seconds
  • The use of custom assertion libraries
  • Exclude tests
  • Nested test suites
  • Test retry support
  • The use of state
  • "should panic" testing
  • Console highlighting
  • Dynamic testing
  • Highlights slow tests
  • No weird macros to try to figure out or debug!
  • Human readable code and test results

Installation

In Cargo.toml:

[dev-dependencies]
laboratory = "2.0.0"

Then in your test files

#[cfg(test)]
mod tests {
    use laboratory::{describe, describe_skip, it, it_skip, it_only, expect};
}

Getting Started

Testing a simple function

// from examples/simple.rs
fn main() {
    add_one(0);
}

// Here we have one function that does
// one thing: Adds one to whatever number
// we pass to it.
fn add_one (n: u64) -> u64 { n + 1 }

#[cfg(test)]
mod tests {

    // lets pull our add_one function into scope
    use super::*;

    // now let's pull in our lab tools into scope
    // to test our function
    use laboratory::{describe, expect, LabResult, NullState};

    // From Rust's perspective we will only define
    // one test, but inside this test we can define
    // however many tests we need.
    #[test]
    fn suite() -> LabResult {

        // let's describe what our add_one() function will do.
        // The describe function takes a closure as its second
        // argument. And that closure also takes an argument which
        // we will call "suite". The argument is the suite's context
        // and it allows for extensive customizations. The context struct
        // comes with a method called it() and using this method we can
        // define a test.
        describe("add_one()", |suite| {

            // when describing what it should do, feel free to be
            // as expressive as you would like.
            suite.it("should return 1 when passed 0", |_| {

                // here we will use the default expect function
                // that comes with laboratory.
                // We expect the result of add_one(0) to equal 1
                expect(add_one(0)).to_equal(1)

            })

            // just as a sanity check, let's add a second test
            .it("should return 2 when passed 1", |_| {

                expect(add_one(1)).to_equal(2)

            });

        }).state(NullState).milis().run()

    }
}

Then run:

$ cargo test -- --nocapture

Result:


running 1 test


### Lab Results Start ###

add_one()
  ✓  should return 1 when passed 0 (0ms)
  ✓  should return 2 when passed 1 (0ms)2 tests completed (0ms)

### Lab Results End ###


test tests::suite ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


Dependencies

~3–4.5MB
~78K SLoC