#test-framework #rspec #test

macro dev speculate

An RSpec inspired minimal testing framework for Rust

30 releases

0.1.2 Apr 23, 2019
0.1.1 Jan 11, 2019
0.1.0 Nov 23, 2018
0.0.27 Jul 10, 2018
0.0.11 Mar 25, 2015

#471 in Testing

Download history 286/week @ 2024-07-21 500/week @ 2024-07-28 303/week @ 2024-08-04 532/week @ 2024-08-11 266/week @ 2024-08-18 423/week @ 2024-08-25 670/week @ 2024-09-01 300/week @ 2024-09-08 247/week @ 2024-09-15 253/week @ 2024-09-22 301/week @ 2024-09-29 176/week @ 2024-10-06 246/week @ 2024-10-13 321/week @ 2024-10-20 363/week @ 2024-10-27 197/week @ 2024-11-03

1,150 downloads per month
Used in fewer than 16 crates

MIT license

17KB
278 lines

speculate.rs Build Status speculate at crates.io

An RSpec inspired minimal testing framework for Rust.

Installation

Add speculate to the dev-dependencies section of your Cargo.toml:

[dev-dependencies]
speculate = "0.1"

And add the following to the top of the Rust file you want to add tests for:

#[cfg(test)]
extern crate speculate;

#[cfg(test)]
use speculate::speculate;  // Must be imported into the current scope.

Usage

Speculate provides the speculate! syntax extension. Inside speculate! { ... }, you can have any "Item", like static, const, fn, etc, and 5 special types of blocks:

  • describe (or its alias context) - to group tests in a hierarchy, for readability. Can be arbitrarily nested.

  • before - contains setup code that's inserted before every sibling and nested it and bench blocks.

  • after - contains teardown code that's inserted after every sibling and nested it and bench blocks.

  • it (or its alias test) - contains tests.

    For example:

    it "can add 1 and 2" {
        assert_eq!(1 + 2, 3);
    }
    

    You can optionally add attributes to this block:

    #[ignore]
    test "ignore" {
        assert_eq!(1, 2);
    }
    
    #[should_panic]
    test "should panic" {
        assert_eq!(1, 2);
    }
    
    #[should_panic(expected = "foo")]
    test "should panic with foo" {
        panic!("foo");
    }
    
  • bench - contains benchmarks.

    For example:

    bench "xor 1 to 1000" |b| {
        b.iter(|| (0..1000).fold(0, |a, b| a ^ b));
    }
    

Complete Example (from tests/example.rs)

extern crate speculate;

use speculate::speculate;

speculate! {
    const ZERO: i32 = 0;

    fn add(a: i32, b: i32) -> i32 {
        a + b
    }

    describe "math" {
        const ONE: i32 = 1;

        fn sub(a: i32, b: i32) -> i32 {
            a - b
        }

        before {
            let two = ONE + ONE;
        }

        it "can add stuff" {
            assert_eq!(ONE, add(ZERO, ONE));
            assert_eq!(two, add(ONE, ONE));
        }

        it "can subtract stuff" {
            assert_eq!(ZERO, sub(ONE, ONE));
            assert_eq!(ONE, sub(two, ONE));
        }
    }
}

License

MIT

Dependencies

~2MB
~48K SLoC