pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.91k stars 2.65k forks source link

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

Open GeorgeFischhof opened 1 month ago

GeorgeFischhof commented 1 month ago

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 commented 1 month ago

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

The-Compiler commented 1 month ago

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.