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

Add public API to get value of fixture by its name #11944

Closed
gryznar opened this issue Feb 7, 2024 · 8 comments
Closed

Add public API to get value of fixture by its name #11944

gryznar opened this issue Feb 7, 2024 · 8 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

@gryznar
Copy link

gryznar commented Feb 7, 2024

What's the problem this feature will solve?

In lower version, it was possible to perform sth like this:

from pytest import FixtureRequest

value = FixtureRequest(item).getfixturevalue('fixture_name')

With pytest 8.0 this is not possible because FixtureRequest is abstract. Public API to get value of fixture would mitigate this problem.

Describe the solution you'd like

The best will be to add method to Item | Function to get fixture value. Alternatively will be to add similar API somewhere in _pytest.fixtures.py and reexport it in pytest.__init__.py
Ability to get value from fixture by its name would be really useful to perform actions which requires fixture at hook level. In my example, I am using driver fixture to pass selenium WebDriver to tests. After test teardown I am taking screenshot if test was failed at pytest_runtest_makereport scope. Lack of available API at pytest level makes it impossible to get fixture if error happens at setup (driver is not present at item.funcargs:

class TestFoo:
    @pytest.fixture(autouse=True)
    def setup(self, driver: WebDriver):
        ...
    
    def test_foo():
        ...

Alternative Solutions

Missing. All code to inspect fixtures is hidden under private API (in _pytest)

@RonnyPfannschmidt
Copy link
Member

the request.getfixturevaluehelper can help there

it was never valid to make a external instance of FixtureRequest

please outline the usecase

@gryznar
Copy link
Author

gryznar commented Feb 7, 2024

Maybe I am unaware of different possibilities to get value of any fixture at hook level. Are there any?

@RonnyPfannschmidt
Copy link
Member

There is no way to just get fixtures at the hook level

Doing so is likely to compromise the consistency of the setup state and worked by accident

@gryznar
Copy link
Author

gryznar commented Feb 7, 2024 via email

@bluetech
Copy link
Member

bluetech commented Feb 8, 2024

How does the driver fixture look like?

@gryznar
Copy link
Author

gryznar commented Feb 8, 2024

Something like this:

from typing import Iterator
from pytest import fixture
from selenium.webdriver import Chrome

@fixture
def driver() -> Iterator[Chrome]:
    driver_ = Chrome()
    yield driver_
    driver_.quit()

@bluetech bluetech 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 Feb 17, 2024
@torotil
Copy link

torotil commented Mar 7, 2024

I have another use-case for this. It’s about committing all the database model instances created in fixtures to the database in one go before invoking the test method: https://stackoverflow.com/a/56013856

@nicoddemus
Copy link
Member

Closing this as duplicate of #2471. 👍

@nicoddemus nicoddemus closed this as not planned Won't fix, can't repro, duplicate, stale Mar 8, 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

5 participants