No description
Find a file
Mark Hildreth 2573779967
Merge pull request #19 from SomeoneToIgnore/patch-1
Use proper World capitalization in the main example
2023-03-29 17:30:18 -04:00
.github/workflows Updated github ci.yml 2021-01-17 22:15:43 -05:00
macros Bumped to 0.1.4 2022-07-19 16:24:32 -04:00
src Documented behavior of AsyncContext with normal function. 2021-02-23 21:02:12 -05:00
tests Copy original argument list directly from input token stream. 2022-07-19 15:46:59 -04:00
.gitignore Initial commit 2021-01-17 15:20:15 -05:00
Cargo.toml Bumped to 0.1.4 2022-07-19 16:24:32 -04:00
LICENSE Initial commit 2021-01-17 15:20:15 -05:00
README.md Use proper World capitalization in the main example 2023-03-28 17:20:03 +03:00
README.tpl Added badges 2021-02-07 00:08:29 -05:00

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