#test #context #test-harness #testing #struct #harness

dev test-context

A library for providing custom setup/teardown for Rust tests without needing a test harness

5 releases

0.1.4 Jul 19, 2022
0.1.3 Feb 27, 2021
0.1.2 Feb 7, 2021
0.1.1 Jan 18, 2021
0.1.0 Jan 18, 2021

#50 in Testing

Download history 1258/week @ 2022-08-17 1224/week @ 2022-08-24 1088/week @ 2022-08-31 922/week @ 2022-09-07 940/week @ 2022-09-14 901/week @ 2022-09-21 1239/week @ 2022-09-28 1654/week @ 2022-10-05 1427/week @ 2022-10-12 1264/week @ 2022-10-19 1540/week @ 2022-10-26 1590/week @ 2022-11-02 1299/week @ 2022-11-09 1536/week @ 2022-11-16 1373/week @ 2022-11-23 1127/week @ 2022-11-30

5,521 downloads per month
Used in fewer than 10 crates

MIT license

8KB

crates.io Documentation License Github

test-context

A library for providing custom setup/teardown for Rust tests without needing a test harness.

use test_context::{test_context, TestContext};

struct MyContext {
    value: String
}

impl TestContext for MyContext {
    fn setup() -> MyContext {
        MyContext {  value: "Hello, world!".to_string() }
    }

    fn teardown(self) {
        // Perform any teardown you wish.
    }
}

#[test_context(MyContext)]
#[test]
fn test_works(ctx: &mut MyContext) {
    assert_eq!(ctx.value, "Hello, world!");
}

Alternatively, you can use async functions in your test context by using the AsyncTestContext.

use test_context::{test_context, AsyncTestContext};

struct MyAsyncContext {
    value: String
}

#[async_trait::async_trait]
impl AsyncTestContext for MyAsyncContext {
    async fn setup() -> MyAsyncContext {
        MyAsyncContext { value: "Hello, world!".to_string() }
    }

    async fn teardown(self) {
        // Perform any teardown you wish.
    }
}

#[test_context(MyAsyncContext)]
fn test_works(ctx: &mut MyAsyncContext) {
    assert_eq!(ctx.value, "Hello, World!");
}

The AsyncTestContext works well with async test wrappers like actix_rt::test or tokio::test.

#[test_context(MyAsyncContext)]
#[tokio::test]
async fn test_works(ctx: &mut MyAsyncContext) {
    assert_eq!(ctx.value, "Hello, World!");
}

License: MIT

Dependencies

~0.9–1.5MB
~31K SLoC