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

rstest try to use case argument as fixture if preceded by underscore #130

Closed
raffomania opened this issue Nov 15, 2021 · 7 comments
Closed

Comments

@raffomania
Copy link

We have a Factory trait for functions that create entities in our database. This trait is implemented for different entities and then applied using rstest_reuse:

#[template]
#[rstest(api)]
#[case::sites(PostFactory {})]
#[case::user_tag_assignments(UserFactory {})]
// ...
pub fn apis<T: Factory>(#[case] api: T) {}

However, this trait only contains associated functions and no state, so we end up calling the functions in tests using assiciated function syntax (simplified example):

#[apply(apis)]
#[tokio::test]
async fn is_correct<T: Factory>(
    api: T,
) -> Result<()> {
    T::create_entity("name", "id").await?;

With this, the compiler complains that the factory argument is unused. When prefixing it with an underscore, the rstest macro seems to generate invalid code:

#[template]
#[rstest(_api)]
#[case::sites(PostFactory {})]
#[case::user_tag_assignments(UserFactory {})]
// ...
pub fn apis<T: Factory>(#[case] _api: T) {}
error[E0433]: failed to resolve: use of undeclared crate or module `api`
   --> mod.rs:321:65
    |
321 | async fn is_correct<T: Factory>(_api: T) -> Result<()> {
    |                                 ^^^^ use of undeclared crate or module `api`

Is there another way to solve this on our end? I'm open to restructuring the code if there's a better way to do it.

Alternatively, it would be great to have support for unused arguments in rstest_reuse templates.

@la10736
Copy link
Owner

la10736 commented Nov 15, 2021

Try follow:

#[template]
#[rstest]
#[case::sites(PostFactory {})]
#[case::user_tag_assignments(UserFactory {})]
pub fn apis<T: Factory>(#[case] api: T) {}

#[apply(apis)]
#[tokio::test]
async fn is_correct<T: Factory>(
    #[case] _api: T,
) -> Result<()> {
    T::create_entity("name", "id").await?;
}

In the first snipped you're mixing compact (old) and attribute (new) syntax. rstest_reuse don't (yet) replicate the function template signature attributes so you need to mark case arguments in any tests.

Maybe I need to update rstest_reuse documentation to explain better how to use it with the newer syntax.

Anyway your original syntax is not wrong but force you to use the same name argument for each tests... that was one of motivation that push me to find a newer syntax.

@raffomania
Copy link
Author

Interesting, I've updated to use the new syntax but still get the exact same error. I can provide a minimal reproduction repo for testing, if you'd like?

@la10736
Copy link
Owner

la10736 commented Nov 15, 2021

Yes, thanks!!

@raffomania
Copy link
Author

Here you go! Running cargo test in the repo should show you the error.

@raffomania
Copy link
Author

Running cargo expand on that example shows the following line:

fn case_1_users() -> () {
        let _api = api::default();
        // ...

This seems to be the source of the error.

@la10736
Copy link
Owner

la10736 commented Nov 15, 2021

That seams a bug in rstest. It's trying to resolve _api as fixture and not as argument:

#[rstest]
#[case::users(UserFactory {})]
pub fn more<T: Factory>(#[case] _api: T) {
    assert_eq!(T::foo(), "foo");
}

Trigger the same error.

Unfortunately you can just do one of

  1. Add #![allow(unused_variables)] at the top of your test file
  2. Add let _ = &api; as first line of your tests

Both of them are really dirty but I cannot fix it in the next days :(

@raffomania
Copy link
Author

Thanks for your help! I'll work around the issue for now. Glad we could pin it down :)

@la10736 la10736 changed the title Using rstest_reuse for generic type parameters rstest try to use case argument as fixture if preceded by underscore Dec 7, 2021
la10736 added a commit that referenced this issue Dec 7, 2021
@la10736 la10736 closed this as completed Dec 7, 2021
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