No description
Find a file
Víctor Martínez 890a3232e9
Merge pull request #38 from JasterV/chore/upgrade-to-0.2.0-version
chore: upgrade to 0.2.0 version
2024-02-26 12:14:32 +01:00
.github Create dependabot.yml 2024-02-25 23:13:20 +01:00
test-context chore: upgrade to 0.2.0 version 2024-02-26 12:12:38 +01:00
test-context-macros chore: upgrade to 0.2.0 version 2024-02-26 12:12:38 +01:00
.gitignore Initial commit 2021-01-17 15:20:15 -05:00
Cargo.toml chore: upgrade to 0.2.0 version 2024-02-26 12:12:38 +01:00
LICENSE Initial commit 2021-01-17 15:20:15 -05:00
README.md refactor: remove support for async-trait crate 2024-02-26 12:05:07 +01: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
}

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