-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Account for changes in fixture dependencies properly #14104
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
Open
Anton3
wants to merge
3
commits into
pytest-dev:main
Choose a base branch
from
Anton3:param-fixtures-deps
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.
+1,396
−106
Open
Changes from all commits
Commits
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 hidden or 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 @@ | ||
| Fixtures are now rebuilt when param changes for a fixture they depend on, if the dependency is via :func:`request.getfixturevalue() <pytest.FixtureRequest.getfixturevalue>`. |
This file contains hidden or 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,3 @@ | ||
| Garbage finalizers for fixture teardown are no longer accumulated in nodes and fixtures. | ||
|
|
||
| :func:`Node.addfinalizer <_pytest.nodes.Node.addfinalizer>` and :func:`request.addfinalizer() <pytest.FixtureRequest.addfinalizer>` now return a handle that allows to remove the finalizer. |
This file contains hidden or 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,63 @@ | ||
| Teardown of parametrized fixtures now happens in the teardown stage of the test before the parameter changes. | ||
|
|
||
| Previously teardown would happen in the setup stage of the test where the parameter changes. | ||
|
|
||
| If a test forces teardown of a parametrized fixture, e.g. using :func:`request.getfixturevalue() <pytest.FixtureRequest.getfixturevalue>`, it instead fails. An example of such test: | ||
|
|
||
| .. code-block:: pytest | ||
|
|
||
| # conftest.py | ||
| import pytest | ||
|
|
||
| @pytest.hookimpl(wrapper=True, tryfirst=True) | ||
| def pytest_collection_modifyitems(items): | ||
| # Disable built-in test reordering. | ||
| original_items = items[:] | ||
| yield | ||
| items[:] = original_items | ||
|
|
||
| # test_invalid.py | ||
| import pytest | ||
|
|
||
| @pytest.fixture(scope="session") | ||
| def foo(request): | ||
| return getattr(request, "param", "default") | ||
|
|
||
| @pytest.mark.parametrize("foo", [1], indirect=True) | ||
| def test_a(foo): | ||
| assert foo == 1 | ||
|
|
||
| def test_b(request): | ||
| request.getfixturevalue("foo") | ||
|
|
||
| @pytest.mark.parametrize("foo", [1], indirect=True) | ||
| def test_c(foo): | ||
| assert foo == 1 | ||
|
|
||
| This produces the following error: | ||
|
|
||
| .. code-block:: console | ||
|
|
||
| Parameter for the requested fixture changed unexpectedly in test: | ||
| test_invalid.py::test_b | ||
| Requested fixture 'foo' defined in: | ||
| test_invalid.py:4 | ||
|
|
||
| Previous parameter value: 1 | ||
| New parameter value: None | ||
|
|
||
| This could happen because the current test requested the previously parametrized | ||
| fixture dynamically via 'getfixturevalue' and did not provide a parameter for the | ||
| fixture. | ||
| Either provide a parameter for the fixture, or make fixture 'foo' statically | ||
| reachable from the current test, e.g. by adding it as an argument to the test | ||
| function. | ||
|
|
||
| Indeed, the following change to ``test_b`` fixes the issue: | ||
|
|
||
| .. code-block:: pytest | ||
|
|
||
| def test_b(request, foo): | ||
| # This is now a little unnecessary as written, but imagine that this dynamic | ||
| # fixture loading happens inside another fixture or inside an utility function. | ||
| request.getfixturevalue("foo") | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.