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

stabilizing fixture APIs (maybe specifically TopRequest) #12630

Open
eqvinox opened this issue Jul 18, 2024 · 0 comments
Open

stabilizing fixture APIs (maybe specifically TopRequest) #12630

eqvinox opened this issue Jul 18, 2024 · 0 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@eqvinox
Copy link

eqvinox commented Jul 18, 2024

related prior reading: #3639

pytest's fixture mechanism is part of pytest's private APIs, to some degree understandably so: referring to a fixture in a test function's parameters is very specific to pytest "native" test functions.

However, there are plugins that could still benefit from having their own ways of referencing/using fixtures, in particular if they provide some kind of environment for tests to be created in that is still Python. (I don't believe there are significant issues with creating fixtures from plugins, at least I'm not seeing any. It is invoking/referencing fixtures I'm concerned with.)

What's the problem this feature will solve?

Since the API is private, it occasionally is changed (most recently a9d1f55), and all plugins that hook into fixtures need to be updated.

Describe the solution you'd like

In the long run, it would be nice for pytest's fixture API to stabilize. This is of course a bit of a moonshot, and there are good arguments that this might be very costly for pytest for very little gain to a small number of plugins.

As a more attainable step, I'd like to suggest removing the @final from _pytest.fixtures.TopRequest (or creating a related class that is part of the public API and can be used) to allow pytest plugins to request fixtures. I think this would fill 99% of plugin fixture request needs — the subrequest machinery feels far less useful to interact with in plugins.

Alternative Solutions

Using pytest internal APIs works, it just creates headaches when things change, and is particularly ugly to deal with if the plugin tries to support multiple versions of pytest (in my case I need to support 7.x and 8.x).

It might also be possible to use Python "meta-hackery", i.e. dynamically constructing functions with the needed fixtures as parameters and then feeding those into pytest's standard machinery to let it fill in fixtures. However, this would involve some quite arcane code and very odd "using things sideways upside-down" interactions with pytest. I've looked at this and given up on it for the time being.

@Zac-HD Zac-HD added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature topic: fixtures anything involving fixtures directly or indirectly labels Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

2 participants