-
Notifications
You must be signed in to change notification settings - Fork 9
Explicitly handle session- and module- scoped fixtures with @saved_fixture
#17
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
Comments
Hi @smarie! Just a feedback that I was so happy that
hmm I think they definitely should, at least theoretically. |
I was giving this a little more thought, and I think we could do at least some of the following things. Either:
I think that I would prefer alternative 2, because then pytest-harvest can still be useful and is up to the user to understand how pytest works. |
Sorry for the delay in answering, I was away - nice proposal indeed.
Yes I was surprised too but apparently there is a "good reason" : if a fixture has two parameters (a, b) and the test order uses a, then b, then a. When current test requires parameter b, then the previously created fixture for parameter a is torn down, even if it is used afterwards again by the following test in the same module. You have probably seen this answer. I personally do not like this idea but their philosophy seems that a fixture should not be "alive" in memory if current test does not use it. That's why the fixture for param 'a' is torn down then recreated in the above example. We should maybe ask for an option to "leave the session and module fixtures alive even if they are not used". That (and only that) will guarantee uniqueness. I mention it at the end of pytest-dev/pytest#3393. |
I suggest to allow That mean that if you have a fixture with session scope used in two tests but not recreated, only the first test id will appear. However if it is recreated, it will appear under each test id where it was recreated. Would that suit your need ? |
Example 1: if the fixture does not change only a single test entry will appear: @pytest.fixture(scope='session')
@saved_fixture
def my_fix(request):
return 1
def test_foo(my_fix):
assert my_fix == 1
def test_bar(my_fix):
assert my_fix == 1
def test_synthesis(fixture_store):
print(fixture_store['my_fix']) The print should contain only one entry with name
|
Example 2: re-creation. If the fixture is instantiated by pytest several times (whatever the reason),there will be an entry for each time it was setup. @pytest.fixture(scope='module', params=[1, 2])
@saved_fixture
def my_fix():
return 1
@pytest.fixture(scope='module', params=['a', 'b'])
@saved_fixture
def my_fix2():
return 1
def test_foo(my_fix):
assert my_fix == 1
def test_bar(my_fix, my_fix2):
assert my_fix == 1
def test_synthesis(fixture_store):
print(list(fixture_store['my_fix'].keys()))
print(list(fixture_store['my_fix2'].keys())) Yields the following "strange but true" pytest execution order as explained by this post:
And as proposed, the fixtures are only stored when they are re-created:
Let me know if that is ok for you. In that case, I'll commit and push this in a new release. |
Hi @smarie, glad you are back! |
As of today
@saved_fixture
can only be used for function-scoped fixtures, but the error message is cryptic:"Internal Error - This fixture 'xxxxxxx' was already stored for test id ''"
We could at least detect this and raise a better exception.
We could also go further and propose specific ways to store session- or module-scoped fixtures.
The main problem is that session-scoped fixtures are not guaranteed to be unique by session, and module-scoped fixtures are not guaranteed to be unique for an unique parameter, in pytest. See pytest-dev/pytest#2846
The text was updated successfully, but these errors were encountered: