-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Deprecate passing iterator argvalues to parametrize #13877
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Using non-:class:`~collections.abc.Collection` iterables (such as generators, iterators, or custom iterable objects) for the ``argvalues`` parameter in :ref:`@pytest.mark.parametrize <pytest.mark.parametrize ref>` and :meth:`metafunc.parametrize <pytest.Metafunc.parametrize>` is now deprecated. | ||
|
|
||
| These iterables get exhausted after the first iteration, | ||
| leading to tests getting unexpectedly skipped in cases such as running :func:`pytest.main()` multiple times, | ||
| using class-level parametrize decorators, | ||
| or collecting tests multiple times. | ||
|
|
||
| See :ref:`parametrize-iterators` for details and suggestions. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,67 @@ Below is a complete list of all pytest features which are considered deprecated. | |||||
| :class:`~pytest.PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`. | ||||||
|
|
||||||
|
|
||||||
| .. _parametrize-iterators: | ||||||
|
|
||||||
| Non-Collection iterables in ``@pytest.mark.parametrize`` | ||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
|
||||||
| .. deprecated:: 9.1 | ||||||
|
|
||||||
| Using non-:class:`~collections.abc.Collection` iterables (such as generators, iterators, or custom iterable objects) | ||||||
| for the ``argvalues`` parameter in :ref:`@pytest.mark.parametrize <pytest.mark.parametrize ref>` | ||||||
| and :meth:`metafunc.parametrize <pytest.Metafunc.parametrize>` is deprecated. | ||||||
|
|
||||||
| These iterables get exhausted after the first iteration, leads to tests getting unexpectedly skipped in cases such as: | ||||||
|
|
||||||
| * Running :func`pytest.main()` multiple times in the same process | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| * Using class-level parametrize decorators where the same mark is applied to multiple test methods | ||||||
| * Collecting tests multiple times | ||||||
|
|
||||||
| Example of problematic code: | ||||||
|
|
||||||
| .. code-block:: python | ||||||
|
|
||||||
| import pytest | ||||||
|
|
||||||
|
|
||||||
| def data_generator(): | ||||||
| yield 1 | ||||||
| yield 2 | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.parametrize("n", data_generator()) | ||||||
| class Test: | ||||||
| def test_1(self, n): | ||||||
| pass | ||||||
|
|
||||||
| # test_2 will be skipped because data_generator() is exhausted. | ||||||
| def test_2(self, n): | ||||||
| pass | ||||||
|
|
||||||
| You can fix it by convert generators and iterators to lists or tuples: | ||||||
|
|
||||||
| .. code-block:: python | ||||||
|
|
||||||
| import pytest | ||||||
|
|
||||||
|
|
||||||
| def data_generator(): | ||||||
| yield 1 | ||||||
| yield 2 | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.parametrize("n", list(data_generator())) | ||||||
| class Test: | ||||||
| def test_1(self, n): | ||||||
| pass | ||||||
|
|
||||||
| def test_2(self, n): | ||||||
| pass | ||||||
|
|
||||||
| Note that :class:`range` objects are ``Collection`` and are not affected by this deprecation. | ||||||
|
|
||||||
|
|
||||||
| .. _monkeypatch-fixup-namespace-packages: | ||||||
|
|
||||||
| ``monkeypatch.syspath_prepend`` with legacy namespace packages | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -93,7 +93,7 @@ def func(x, y): | |||||||
| metafunc.parametrize("y", [5, 6]) | ||||||||
|
|
||||||||
| with pytest.raises(TypeError, match=r"^ids must be a callable or an iterable$"): | ||||||||
| metafunc.parametrize("y", [5, 6], ids=42) # type: ignore[arg-type] | ||||||||
| metafunc.parametrize("y", [5, 6], ids=42) # type: ignore[call-overload] | ||||||||
|
|
||||||||
| def test_parametrize_error_iterator(self) -> None: | ||||||||
| def func(x): | ||||||||
|
|
@@ -134,7 +134,7 @@ def func(x): | |||||||
| fail.Exception, | ||||||||
| match=r"parametrize\(\) call in func got an unexpected scope value 'doggy'", | ||||||||
| ): | ||||||||
| metafunc.parametrize("x", [1], scope="doggy") # type: ignore[arg-type] | ||||||||
| metafunc.parametrize("x", [1], scope="doggy") # type: ignore[call-overload] | ||||||||
|
|
||||||||
| def test_parametrize_request_name(self, pytester: Pytester) -> None: | ||||||||
| """Show proper error when 'request' is used as a parameter name in parametrize (#6183)""" | ||||||||
|
|
@@ -813,7 +813,7 @@ def func(x, y): | |||||||
| fail.Exception, | ||||||||
| match="In func: expected Sequence or boolean for indirect, got dict", | ||||||||
| ): | ||||||||
| metafunc.parametrize("x, y", [("a", "b")], indirect={}) # type: ignore[arg-type] | ||||||||
| metafunc.parametrize("x, y", [("a", "b")], indirect={}) # type: ignore[call-overload] | ||||||||
|
|
||||||||
| def test_parametrize_indirect_list_functional(self, pytester: Pytester) -> None: | ||||||||
| """ | ||||||||
|
|
@@ -1123,6 +1123,24 @@ def test_3(self, arg, arg2): | |||||||
| """ | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_parametrize_iterator_deprecation(self) -> None: | ||||||||
| """Test that using iterators for argvalues raises a deprecation warning.""" | ||||||||
|
|
||||||||
| def func(x): | ||||||||
| pass | ||||||||
|
|
||||||||
| def data_generator(): | ||||||||
| yield 1 | ||||||||
| yield 2 | ||||||||
|
|
||||||||
| metafunc = self.Metafunc(func) | ||||||||
|
|
||||||||
| with pytest.warns( | ||||||||
| pytest.PytestRemovedIn10Warning, | ||||||||
| match=r"The argvalues parameter.*uses a non-Collection iterable", | ||||||||
| ): | ||||||||
| metafunc.parametrize("x", data_generator()) | ||||||||
|
|
||||||||
|
|
||||||||
| class TestMetafuncFunctional: | ||||||||
| def test_attributes(self, pytester: Pytester) -> None: | ||||||||
|
|
@@ -1682,6 +1700,62 @@ def test_3(self, fixture): | |||||||
| ] | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_parametrize_generator_multiple_runs(self, pytester: Pytester) -> None: | ||||||||
| """Test that generators in parametrize work with multiple pytest.main() (deprecated).""" | ||||||||
| pytester.makepyfile( | ||||||||
| """ | ||||||||
| import pytest | ||||||||
|
|
||||||||
| def data_generator(): | ||||||||
| yield 1 | ||||||||
| yield 2 | ||||||||
|
|
||||||||
| @pytest.mark.parametrize("bar", data_generator()) | ||||||||
| def test_foo(bar): | ||||||||
| pass | ||||||||
|
|
||||||||
| if __name__ == '__main__': | ||||||||
| args = ["-q", "--collect-only"] | ||||||||
| pytest.main(args) # First run - should work with warning | ||||||||
| pytest.main(args) # Second run - should also work with warning | ||||||||
| """ | ||||||||
| ) | ||||||||
| result = pytester.runpytest("-W", "default") | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean:
Suggested change
|
||||||||
| # Should see the deprecation warnings. | ||||||||
| result.stdout.fnmatch_lines( | ||||||||
| [ | ||||||||
| "*PytestRemovedIn10Warning: The argvalues parameter*", | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand we should see this appear twice?
Suggested change
|
||||||||
| ] | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_parametrize_iterator_class_multiple_tests( | ||||||||
| self, pytester: Pytester | ||||||||
| ) -> None: | ||||||||
| """Test that iterators in parametrize on a class get exhausted (deprecated).""" | ||||||||
| pytester.makepyfile( | ||||||||
| """ | ||||||||
| import pytest | ||||||||
|
|
||||||||
| @pytest.mark.parametrize("n", iter(range(2))) | ||||||||
| class Test: | ||||||||
| def test_1(self, n): | ||||||||
| pass | ||||||||
|
|
||||||||
| def test_2(self, n): | ||||||||
| pass | ||||||||
| """ | ||||||||
| ) | ||||||||
| result = pytester.runpytest("-v", "-W", "default") | ||||||||
| # Iterator gets exhausted after first test, second test gets no parameters. | ||||||||
| # This is deprecated. | ||||||||
| result.assert_outcomes(passed=2, skipped=1) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be made more clear and resilient by testing the node ids themselves as output by |
||||||||
| # Should see the deprecation warnings. | ||||||||
| result.stdout.fnmatch_lines( | ||||||||
| [ | ||||||||
| "*PytestRemovedIn10Warning: The argvalues parameter*", | ||||||||
| ] | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
| class TestMetafuncFunctionalAuto: | ||||||||
| """Tests related to automatically find out the correct scope for | ||||||||
|
|
||||||||
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.