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

No API to request fixtures from pytest.Item #2471

Open
cjw296 opened this issue Jun 5, 2017 · 26 comments
Open

No API to request fixtures from pytest.Item #2471

cjw296 opened this issue Jun 5, 2017 · 26 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

@cjw296
Copy link

cjw296 commented Jun 5, 2017

It made me a bit sad that I had to c'n'p this piece of code, rather than call a helper, to request fixtures in an Item subclass:

https://github.com/cjw296/sybil/blob/552d3ff2d6e8639ee5d3c8efa26f5e535e064ae8/sybil/integration/pytest.py#L41

@nicoddemus
Copy link
Member

That's a little clumsy I agree. Unfortunately I'm not sure we want to expose the current fixture API as official right now, given that time and time again we mention that this part of the code could use some refactoring...

@cjw296

This comment was marked as off-topic.

@RonnyPfannschmidt

This comment was marked as off-topic.

@nicoddemus

This comment was marked as off-topic.

@nicoddemus nicoddemus changed the title Need to C&P code to request fixtures No API to request fixtures from pytest.Item Jun 6, 2017
@nicoddemus nicoddemus added 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 labels Sep 28, 2017
@Zac-HD

This comment was marked as off-topic.

@Zac-HD Zac-HD closed this as completed Feb 25, 2019
@cjw296

This comment was marked as off-topic.

@RonnyPfannschmidt

This comment was marked as outdated.

@Zac-HD

This comment was marked as off-topic.

@Zac-HD Zac-HD reopened this Feb 25, 2019
@cjw296

This comment was marked as off-topic.

@cjw296

This comment was marked as off-topic.

@nicoddemus
Copy link
Member

Sorry - I'm still finding the right balance for that.

No worries! I for one definitely appreciate your periodic triage of the issue tracker. We also try to find the right balance, don't worry. And we can always re-open an issue if we find that's the correct course of action.

@oprypin
Copy link

oprypin commented Oct 23, 2020

I just ran into this issue. I want to use some functionality that's only available in fixtures (namely, capture logs) inside a test implemented as pytest.Item.runtest being generated by pytest_collect_file.

@The-Compiler
Copy link
Member

Some fallout from #11868:

What's the current state on:

Unfortunately I'm not sure we want to expose the current fixture API as official right now, given that time and time again we mention that this part of the code could use some refactoring...

Given that IIRC we had a lot of refactorings to fixture implementations recently? @bluetech @sadra-barikbin

@RonnyPfannschmidt
Copy link
Member

the large, awesome and numerous refactorings @bluetech and @sadra-barikbin poured in over the last few months,
are but the tip of the iceberg

i'm still very terrified of opening the api up, as i'm aware of a lot of the backlogs and pains ever since the 2016 sprint
i wish there was more time and attention that i could pour into the topic as well - the progressions that @sadra-barikbin and @bluetech brought in are very uplifting to say the least

paquiteau added a commit to mind-inria/mri-nufft that referenced this issue Jan 29, 2024
@nicoddemus
Copy link
Member

i'm still very terrified of opening the api up

While I share that fear, we probably don't need/should not just open the API, we can probably start small with functions/methods which are based on pytest's external design, rather than exposing implementation details.

Just an example from the top of my head, we could introduce a method to pytest.Item which returns a tree-like structure with all the fixture values active on it. This does not reflect the internal state of things, but seems like it would be stable enough regardless of changes in implementation: pytest will always have the notion of fixtures active on an item, and fixture closures.

@bluetech
Copy link
Member

@cjw296 Can you describe what "request fixtures from pytest.Item" means? It is not very clear. I assume it's not literally requesting a fixture, since request.getfixturevalue() exists.

@RonnyPfannschmidt
Copy link
Member

@bluetech i believe it relates to the initially linked code dealing with non-python items

as far as fixture configuration, parameterization and state management is concerned, a lot of it is has so far primarily stuck on python details

@bluetech
Copy link
Member

as far as fixture configuration, parameterization and state management is concerned, a lot of it is has so far primarily stuck on python details

Right, I'm mostly interested to know which fixture functionality these non-python items want to use (i.e. use cases), and what they currently implement using the pytest internals.

@RonnyPfannschmidt
Copy link
Member

The initially linked code shows what they do

@cjw296
Copy link
Author

cjw296 commented Jan 31, 2024

@bluetech - ultimately, I need to do this:

class SybilItem(pytest.Item):

...

    def setup(self) -> None:
        for name, fixture in ...:
            self.example.namespace[name] = fixture

...but to do that, I currently need to do this:

https://github.com/simplistix/sybil/blob/0fc6f9cca420d46481e0e4bf3668b40f38034abc/sybil/integration/pytest.py#L51-L70

...and then this:

https://github.com/simplistix/sybil/blob/0fc6f9cca420d46481e0e4bf3668b40f38034abc/sybil/integration/pytest.py#L84-L87

If there's a better, more supported way to do this, I would love a PR to show me how!

@bluetech
Copy link
Member

What is fixture in this example?

@cjw296
Copy link
Author

cjw296 commented Jan 31, 2024

What is fixture in this example?

The instance of whatever the fixture is.

For example, if this were a normal pytest function:

def test_foo(bar: Baz) -> None:
    ...

...then name='bar' and fixture is an instance of type Baz.

paquiteau added a commit to mind-inria/mri-nufft that referenced this issue Feb 2, 2024
@bluetech
Copy link
Member

A note: while supporting basic fixtures is one thing and probably possible, supporting parametrization will be quite a bit more difficult. Even for pytest's own doctests it's broken -- the following errors out:

import pytest

@pytest.fixture(autouse=True, params=[1, 2])
def number(request, doctest_namespace):
    doctest_namespace["number"] = request.param

def func():
    """
    >>> number < 10
    True
    """

@nicoddemus
Copy link
Member

A note: while supporting basic fixtures is one thing and probably possible, supporting parametrization will be quite a bit more difficult.

I think it is OK to only support basic fixtures initially, with an error if a parametrized fixture is requested. This will probably already cover the majority of cases.

@gryznar
Copy link

gryznar commented Mar 8, 2024

+1 for adding this API

@RonnyPfannschmidt
Copy link
Member

The function definition type needs to be hoisted into nodes

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

8 participants