mirror of
https://codeberg.org/JasterV/test-context.git
synced 2026-04-26 18:10:06 +00:00
2.1 KiB
2.1 KiB
Change of Maintainership
I have stepped down as maintainer of this package. https://github.com/JasterV has gracciously offered to step in as the new maintainer. The new official repository is at https://github.com/JasterV/test-context.
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