pytest icon indicating copy to clipboard operation
pytest copied to clipboard

It would be good to have fixtures with dinamically changed / different scopes

Open GeorgeFischhof opened this issue 1 year ago • 2 comments

What's the problem this feature will solve?

I create random users for the tests. I use factories according to https://docs.pytest.org/en/latest/how-to/fixtures.html#factories-as-fixtures

The user creation is quite expensive (in time), so I would like to create only one user for negative test cases, and one for every positive test case when the user state is changed during the test.

My idea was that create user for negative tests with module scope and use function scope for positive ones. But it is not possible. (I duplicated the fixtures with different scopes, but it is not good)

Describe the solution you'd like

I do not know how to solve this

Alternative Solutions

As I wrote this feature request, it got to my mind that there is a class scope, and that can solve this.: Create a class for negative test cases and one class for each positive, and it solves the issue.

Perhaps documenting this with an example (two class type: one for all negative test cases and several for the positive ones) should be enough / good

BR, George

Additional context

GeorgeFischhof avatar Jul 31 '24 12:07 GeorgeFischhof

Hi, In the meantime it's got to mind that if I use managed version (closure created, and function returned from fixture) then users will be created for every test function, so the time consuming thing remains.

BR, George

GeorgeFischhof avatar Aug 07 '24 06:08 GeorgeFischhof

This sounds a lot like #1681 to me - unfortunately there was unforeseen trouble with the implementation of that and we needed to revert it again.

The-Compiler avatar Aug 07 '24 07:08 The-Compiler