rstest icon indicating copy to clipboard operation
rstest copied to clipboard

Enable injecting test runner

Open la10736 opened this issue 5 years ago • 4 comments

We provide a way to define a test runner to use instead the default one (test or async-std::test for async function).

If the given attributes ends with test (the last segment in Path) we don't add test attribute to every test but just leave the provided one.

By this approach we can give a way to use rstest with a lot of other tools without write an ad hoc support.

As example follow code just works without care about what actix_rt::test.

#[rstest(a, b, case (1, 2), case(3, 4))]
#[actix_rt::test]
async fn foo(a: u32, b: u32) {}

la10736 avatar Apr 09 '20 20:04 la10736

  • [x] Impl
  • [x] Integration test
  • [x] Unit tests
  • [x] Changelog
  • [ ] Docs

la10736 avatar Apr 09 '20 20:04 la10736

Does not work for #[rocket::async_test]

DanielJoyce avatar May 18 '21 22:05 DanielJoyce

Unfortunately this case is not supported yet. "Just the attributes that ends with test (last path segment) can be injected." In this case last path segment is async_test and not just test.

I can try to think to something looser or add some custom rules... I've not a clear position about this :(

la10736 avatar May 19 '21 06:05 la10736

This seems to work out of the box already but I'm not sure it works with macros that also require parameters. My use case is wanting to rstest fixtures in conjunction with a sqlx macro: https://docs.rs/sqlx/0.6.1/sqlx/attr.test.html

I'm not sure how the rstest macro works but it looks like it inspects all parameters, is there a way to perhaps skip one? For example:

#[fixture]
async fn my_fixture() -> FixtureStruct { FixtureStruct::default() }

#[rstest]
#[sqlx::test]
async fn test_db(#[future] my_fixture: &FixtureStruct, pool: PgPool) {
    // test
}

This will fail because rstest uses the pool parameter but there is no such fixture:

error[E0433]: failed to resolve: use of undeclared crate or module `pool`
   |
60 | async fn test_db(#[future] my_fixture: App, pool: PgPool) {
   |                                                                             ^^^^ use of undeclared crate or module `pool`

rminderhoud avatar Aug 17 '22 01:08 rminderhoud