Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async / tokio::test macro Support? #8

Closed
leeola opened this issue May 26, 2023 · 2 comments
Closed

Async / tokio::test macro Support? #8

leeola opened this issue May 26, 2023 · 2 comments

Comments

@leeola
Copy link

leeola commented May 26, 2023

Not sure how tightly this library is coupled to proptest or proptest_derive, but perhaps related to proptest-rs/proptest#179 it would be great if we could use this with async tests.

@frozenlib
Copy link
Owner

#[test_strategy::proptest] expands to proptest::proptest! { ... }, so if proptest! { } supports async fn, test_strategy will also be able to support async fn.

@frozenlib
Copy link
Owner

I have experimentally implemented support for async fn in fd49486.

You can write tests for async fn by adding a dependency on tokio to Cargo.toml and adding async = "tokio" to the argument of #[test_strategy::proptest] as shown below.

[dev-dependencies]
proptest = "1.1.0"
tokio = { version = "1.28.1", features = ["rt-multi-thread"] }
test-strategy = { git = "https://github.com/frozenlib/test-strategy.git" }
#[test_strategy::proptest(async = "tokio")]
async fn test() {
	// todo
}

The above code generates the following code.

proptest::proptest! {
    #[test]
    fn test() {
        let ret: ::core::result::Result<_, ::proptest::test_runner::TestCaseError> =
            tokio::runtime::Runtime::new()
                .unwrap()
                .block_on(async move {
                    {
                    	// todo
                    }
                    Ok(())
                });
        ret?;
    }
}

I am not sure if this is the right way to do it, as block_on will be called repeatedly, but I believe that if we try to do more than this, we will need to support async fn by proptest::proptest!{ }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants