#spec #testing

specit

Rust测试中的"Spec 'it'"

3个版本 (重大更新)

0.4.0 2021年1月9日
0.3.0 2020年9月10日
0.2.0 2020年9月9日

#1186过程宏

Download history 89/week @ 2024-03-31 146/week @ 2024-04-07 102/week @ 2024-04-14 12/week @ 2024-04-21 31/week @ 2024-04-28 90/week @ 2024-05-05 16/week @ 2024-05-12 26/week @ 2024-05-19 95/week @ 2024-05-26 66/week @ 2024-06-02 126/week @ 2024-06-09 46/week @ 2024-06-16 61/week @ 2024-06-23 55/week @ 2024-06-30 29/week @ 2024-07-07 94/week @ 2024-07-14

每月240次下载

MIT 许可证

9KB
114

specit

Crates.io CI

Rust测试中的"Spec 'it'"

安装

# Cargo.toml

[dev-dependencies]
specit = "0.3.0"

用法

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);
}

测试输出如下。

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);
    }
}

使用describe的测试输出如下。

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

你可以使用#[tokio::test]测试异步函数。

use specit::it;

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

你可以在每个异步运行时使用以下特性来简化你的代码。

特性= ["tokio"]

你可以使用use specit::tokio_it as it来测试异步函数,而不需要使用#[tokio::test],如下所示。

use specit::tokio_it as it;

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

特性= ["async-std"]

使用#[it(...)]代替#[async_std::test],如下所示。

use specit::async_std_it as it;

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

特性= ["lib-wasm-bindgen"]

使用#[it(...)]代替#[wasm_bindgen_test::wasm_bindgen_test],如下所示。

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);
}

内部

内部,上述函数是 should_be_correct()should_be_wrong()。您可以使用任何字符串。非字母数字字符将被编码为 '_'

依赖项

~1–13MB
~162K SLoC