No description
Find a file
Mark Hildreth 418ed39c1e Allow return of wrapped functions value.
This allows the macro to return the value of the function being
annotated. For most testing, this probably shouldn't matter, but
considering it's simple enough to do I decided to add it in in case any
other test wrappers require it.
2021-01-17 22:02:19 -05:00
.github/workflows Initial version 2021-01-17 21:23:34 -05:00
macros Allow return of wrapped functions value. 2021-01-17 22:02:19 -05:00
src Initial version 2021-01-17 21:23:34 -05:00
tests Allow return of wrapped functions value. 2021-01-17 22:02:19 -05:00
.gitignore Initial commit 2021-01-17 15:20:15 -05:00
Cargo.toml Initial version 2021-01-17 21:23:34 -05:00
LICENSE Initial commit 2021-01-17 15:20:15 -05:00
README.md Initial version 2021-01-17 21:23:34 -05:00

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

Works with other test wrappers like actix_rt::test or tokio::test that turn your test function into an async function.

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 teradown you wish.
    }
}

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