3 releases (breaking)

0.4.0 Jan 9, 2021
0.3.0 Sep 10, 2020
0.2.0 Sep 9, 2020

#1934 in Procedural macros

Download history 26/week @ 2024-07-21 105/week @ 2024-07-28 143/week @ 2024-08-04 51/week @ 2024-08-11 8/week @ 2024-08-18 30/week @ 2024-08-25 49/week @ 2024-09-01 124/week @ 2024-09-08 60/week @ 2024-09-15 36/week @ 2024-09-22 82/week @ 2024-09-29 125/week @ 2024-10-06 34/week @ 2024-10-13 129/week @ 2024-10-20 71/week @ 2024-10-27 79/week @ 2024-11-03

340 downloads per month

MIT license

9KB
114 lines

specit

Crates.io CI

Spec "it" for Rust testing

Install

# Cargo.toml

[dev-dependencies]
specit = "0.3.0"

Usage

use specit::it;

#[it("should be correct")]
fn t() {
    assert_eq!(2 + 2, 4);
}

#[it("should be wrong")]
#[should_panic]
fn t() {
    assert_eq!(1 + 1, 3);
}

The test output is like the following.

running 2 tests
test should_be_correct ... ok
test should_be_wrong ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

describe

use specit::describe;

#[describe("arithmetic operations")]
mod m {
    use specit::it;

    #[it("should add two numbers")]
    pub fn t() {
        assert_eq!(2 + 2, 4);
    }

    #[it("should multiple two numbers")]
    pub fn t() {
        assert_eq!(3 * 3, 9);
    }
}

The test output with describe is like the following.

running 2 tests
test arithmetic_operations::should_add_two_numbers ... ok
test arithmetic_operations::should_multiple_two_numbers ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

#tokio::test support

You can test with #[tokio::test] for asynchronous functions.

use specit::it;

#[it("should work with tokio::test")]
#[tokio::test]
async fn t() {
    let f = async { 10 };
    assert_eq!(f.await, 10);
}

You can get short your code using the following features in each asynchronous runtime.

features = ["tokio"]

You can use use specit::tokio_it as it for testing asynchronous functions without #[tokio::test] like the following.

use specit::tokio_it as it;

#[it("should work with tokio")]
async fn t() {
    let f = async { 10 };
    assert_eq!(f.await, 10);
}

features = ["async-std"]

Use #[it(...)] instead of #[async_std::test] as follows.

use specit::async_std_it as it;

#[it("should be correct")]
async fn t() {
    let f = async { 10 };
    assert_eq!(f.await, 10);
}

features = ["lib-wasm-bindgen"]

Use #[it(...)] instead of #[wasm_bindgen_test::wasm_bindgen_test] as follows.

use specit::wasm_bindgen_test_it as it;
use wasm_bindgen::prelude::JsValue;
use wasm_bindgen_futures::JsFuture;

#[it("should be correct")]
async fn t() {
    let promise = js_sys::Promise::resolve(&JsValue::from(42));
    let x = JsFuture::from(promise).await.unwrap();
    assert_eq!(x, 42);
}

Internal

Internally, the functions above are should_be_correct() and should_be_wrong(). You can use any string. Non-alphanum characters are encoded into '_'.

Dependencies

~1–13MB
~167K SLoC