A library for providing custom setup/teardown for Rust tests without needing a test harness.
```rust use testcontext::{testcontext, 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.
}
}
fn testworks(ctx: &mut MyContext) { asserteq!(ctx.value, "Hello, world!"); } ```
Alternatively, you can use async
functions in your test context by using the
AsyncTestContext
.
```rust use testcontext::{testcontext, 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.
}
}
fn testworks(ctx: &mut MyAsyncContext) { asserteq!(ctx.value, "Hello, World!"); } ```
The AsyncTestContext
works well with async test wrappers like
actix_rt::test
or
tokio::test
.
```rust
async fn testworks(ctx: &mut MyAsyncContext) { asserteq!(ctx.value, "Hello, World!"); } ```
License: MIT