2727//! Alternatively, you can use `async` functions in your test context by using the
2828//! `AsyncTestContext`.
2929//!
30- //! ```
30+ //! ```no_run
3131//! use test_context::{test_context, AsyncTestContext};
3232//!
3333//! struct MyAsyncContext {
3434//! value: String
3535//! }
3636//!
37- //! #[async_trait::async_trait]
3837//! impl AsyncTestContext for MyAsyncContext {
3938//! async fn setup() -> MyAsyncContext {
4039//! MyAsyncContext { value: "Hello, world!".to_string() }
4645//! }
4746//!
4847//! #[test_context(MyAsyncContext)]
48+ //! #[test]
4949//! fn test_works(ctx: &mut MyAsyncContext) {
5050//! assert_eq!(ctx.value, "Hello, World!");
5151//! }
5555//! [`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
5656//! [`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html).
5757//!
58- //! ```
59- //! # use test_context::{test_context, AsyncTestContext};
60- //! # struct MyAsyncContext {
61- //! # value: String
62- //! # }
63- //! # #[async_trait::async_trait]
64- //! # impl AsyncTestContext for MyAsyncContext {
65- //! # async fn setup() -> MyAsyncContext {
66- //! # MyAsyncContext { value: "Hello, world!".to_string() }
67- //! # }
68- //! # async fn teardown(self) {
69- //! # // Perform any teardown you wish.
70- //! # }
71- //! # }
58+ //! ```no_run
59+ //! use test_context::{test_context, AsyncTestContext};
60+ //!
61+ //! struct MyAsyncContext {
62+ //! value: String
63+ //! }
64+ //!
65+ //! impl AsyncTestContext for MyAsyncContext {
66+ //! async fn setup() -> MyAsyncContext {
67+ //! MyAsyncContext { value: "Hello, world!".to_string() }
68+ //! }
69+ //! async fn teardown(self) {
70+ //! // Perform any teardown you wish.
71+ //! }
72+ //! }
73+ //!
7274//! #[test_context(MyAsyncContext)]
7375//! #[tokio::test]
7476//! async fn test_async_works(ctx: &mut MyAsyncContext) {
@@ -95,17 +97,18 @@ where
9597}
9698
9799/// The trait to implement to get setup/teardown functionality for async tests.
98- #[ async_trait:: async_trait]
99100pub trait AsyncTestContext
100101where
101102 Self : Sized ,
102103{
103104 /// Create the context. This is run once before each test that uses the context.
104- async fn setup ( ) -> Self ;
105+ fn setup ( ) -> impl std :: future :: Future < Output = Self > + Send ;
105106
106107 /// Perform any additional cleanup of the context besides that already provided by
107108 /// normal "drop" semantics.
108- async fn teardown ( self ) { }
109+ fn teardown ( self ) -> impl std:: future:: Future < Output = ( ) > + Send {
110+ async { }
111+ }
109112}
110113
111114// Automatically impl TestContext for anything Send that impls AsyncTestContext.
0 commit comments