-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 cache fixtures POC #12816
Draft
niroshaimos
wants to merge
4
commits into
pytest-dev:main
Choose a base branch
from
niroshaimos:no-cache-fixture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
no cache fixtures POC #12816
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from __future__ import annotations | ||
|
||
from _pytest.pytester import Pytester | ||
|
||
|
||
def test_setup_teardown_executed_for_every_fixture_usage_without_caching( | ||
pytester: Pytester, | ||
) -> None: | ||
pytester.makepyfile( | ||
""" | ||
import pytest | ||
import logging | ||
|
||
@pytest.fixture(scope="invocation") | ||
def fixt(): | ||
logging.info("&&Setting up fixt&&") | ||
yield | ||
logging.info("&&Tearing down fixt&&") | ||
|
||
|
||
@pytest.fixture() | ||
def a(fixt): | ||
... | ||
|
||
|
||
@pytest.fixture() | ||
def b(fixt): | ||
... | ||
|
||
|
||
def test(a, b, fixt): | ||
assert False | ||
""" | ||
) | ||
|
||
result = pytester.runpytest("--log-level=INFO") | ||
assert result.ret == 1 | ||
result.stdout.fnmatch_lines( | ||
[ | ||
*["*&&Setting up fixt&&*"] * 3, | ||
*["*&&Tearing down fixt&&*"] * 3, | ||
] | ||
) | ||
|
||
|
||
def test_setup_teardown_executed_for_every_getfixturevalue_usage_without_caching( | ||
pytester: Pytester, | ||
) -> None: | ||
pytester.makepyfile( | ||
""" | ||
import pytest | ||
import logging | ||
|
||
@pytest.fixture(scope="invocation") | ||
def fixt(): | ||
logging.info("&&Setting up fixt&&") | ||
yield | ||
logging.info("&&Tearing down fixt&&") | ||
|
||
|
||
def test(request): | ||
random_nums = [request.getfixturevalue('fixt') for _ in range(3)] | ||
assert False | ||
""" | ||
) | ||
result = pytester.runpytest("--log-level=INFO") | ||
assert result.ret == 1 | ||
result.stdout.fnmatch_lines( | ||
[ | ||
*["*&&Setting up fixt&&*"] * 3, | ||
*["*&&Tearing down fixt&&*"] * 3, | ||
] | ||
) | ||
|
||
|
||
def test_non_cached_fixture_generates_unique_values_per_usage( | ||
pytester: Pytester, | ||
) -> None: | ||
pytester.makepyfile( | ||
""" | ||
import pytest | ||
|
||
@pytest.fixture(scope="invocation") | ||
def random_num(): | ||
import random | ||
return random.randint(-100_000_000_000, 100_000_000_000) | ||
|
||
|
||
@pytest.fixture() | ||
def a(random_num): | ||
return random_num | ||
|
||
|
||
@pytest.fixture() | ||
def b(random_num): | ||
return random_num | ||
|
||
|
||
def test(a, b, random_num): | ||
assert a != b != random_num | ||
""" | ||
) | ||
pytester.runpytest().assert_outcomes(passed=1) | ||
|
||
|
||
def test_non_cached_fixture_generates_unique_values_per_getfixturevalue_usage( | ||
pytester: Pytester, | ||
) -> None: | ||
pytester.makepyfile( | ||
""" | ||
import pytest | ||
|
||
@pytest.fixture(scope="invocation") | ||
def random_num(): | ||
import random | ||
yield random.randint(-100_000_000_000, 100_000_000_000) | ||
|
||
|
||
def test(request): | ||
random_nums = [request.getfixturevalue('random_num') for _ in range(3)] | ||
assert random_nums[0] != random_nums[1] != random_nums[2] | ||
""" | ||
) | ||
pytester.runpytest().assert_outcomes(passed=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as mentioned in the issue before, i think its important to use a scope name here as indicator instead of the new boolean (in particular as use_cache as implemented here invalidates the scope
the scope name we had in mind back in 2020 was "invocation" to mean that the scope of a validity for a fixture value would be the call (be it other fixture or test function in which it was used
i like the hack with just popping the values as a workaround for the current tech debt as it seems like the impact is limited, but i'd like the input of the others in this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.
If we insist on going with the scope like approach rather than the boolean, I think we could still work-around this while leaving the implementation the same. We just change the arg name to a string scope and will currently only support one scope for this (or two considering default behavior).
This way changing the implementation to a more robust infrastructure centered around the actual scoping won't be breaking.
I think this would be a step in the right direction and would probably solve the general use-case for this issue :)
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the rough idea - add a new scope name, then use the workaround you created and gate on the scope name
For good measure we might need a test that ensures setup/teardown is in sync with the usage sites
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the rough changes as I understand the request.
I have added a new scope
Invocation
which will be a scope lower thanFunction
(of course it's cleanup scope will occur duringFunction
cleanup level)Of course if this is the direction we will be going with I'll move the tests to
test_scope.py
When it comes to coverage of the feature, I believe what I have created should be sufficient. I am not sure I understand the additional test suggestions you have provided. If you could expand on it, that would be great.
Please take a look and tell me what you think :)