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

change param_index if param is pseudofixturedef #12082

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/_pytest/python.py
Expand Up @@ -1326,10 +1326,12 @@
# more than once) then we accumulate those calls generating the cartesian product
# of all calls.
newcalls = []
for callspec in self._calls or [CallSpec2()]:
for callspec_index, callspec in enumerate(self._calls or [CallSpec2()]):
for param_index, (param_id, param_set) in enumerate(
zip(ids, parametersets)
):
if name2pseudofixturedef:
param_index = callspec_index + param_index

Check warning on line 1334 in src/_pytest/python.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python.py#L1334

Added line #L1334 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have

Suggested change
param_index = callspec_index + param_index
param_index = callspec_index * len(parametersets) + param_index

? Because currently some callspecs would have arguments with the same param index but different param values. For example, test1[1-b] and test1[2-a] whose arg2 param index is 1. However this doesn't show up in this test. In

def test_multi_parametrize_reordering(self, pytester: Pytester) -> None:
        pytester.makepyfile(
            """
            import pytest

            @pytest.mark.parametrize("arg2", ['a', 'b', 'c'], scope='class')
            @pytest.mark.parametrize("arg1", [1, 2], scope='class')
            class TestClass:
                def test1(self, arg1, arg2):
                    pass

                def test2(self, arg1, arg2):
                    pass
        """
        )
        result = pytester.runpytest("--collect-only")
        result.stdout.re_match_lines(
            [
                r"      <Function test1\[1-a\]>",
                r"      <Function test2\[1-a\]>",
                r"      <Function test1\[1-b\]>",
                r"      <Function test2\[1-b\]>",
                r"      <Function test1\[1-c\]>",
                r"      <Function test2\[1-c\]>",
                r"      <Function test1\[2-a\]>",
                r"      <Function test2\[2-a\]>",
                r"      <Function test1\[2-b\]>",
                r"      <Function test2\[2-b\]>",
                r"      <Function test1\[2-c\]>",
                r"      <Function test2\[2-c\]>",
            ]
        )

it raises error. The test output would be:

<Dir test_multi_parametrize_reordering0>
  <Module test_multi_parametrize_reordering.py>
    <Class TestClass>
      <Function test1[1-a]>
      <Function test2[1-a]>
      <Function test1[1-b]>
      <Function test2[1-b]>
      <Function test1[2-a]>
      <Function test2[2-a]>
      <Function test1[2-b]>
      <Function test2[2-b]>
      <Function test1[1-c]>
      <Function test2[1-c]>
      <Function test1[2-c]>
      <Function test2[2-c]>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also suggest removing the "if name2pseudofixturedef" check so that sorting parameterized tests through a marker and through a fixture works the same way.
But then it is necessary to correct the tests test_module_parametrized_ordering and test_dynamic_parametrized_ordering along the way

newcallspec = callspec.setmulti(
argnames=argnames,
valset=param_set.values,
Expand Down
29 changes: 29 additions & 0 deletions testing/python/metafunc.py
Expand Up @@ -1019,6 +1019,35 @@ def test3(arg1):
]
)

def test_multi_parametrize_reordering(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest

@pytest.mark.parametrize("arg2", ['a', 'b'], scope='class')
@pytest.mark.parametrize("arg1", [1, 2], scope='class')
class TestClass:
def test1(self, arg1, arg2):
pass

def test2(self, arg1, arg2):
pass
"""
)
result = pytester.runpytest("--collect-only")
result.stdout.re_match_lines(
[
r" <Function test1\[1-a\]>",
r" <Function test2\[1-a\]>",
r" <Function test1\[1-b\]>",
r" <Function test2\[1-b\]>",
r" <Function test1\[2-a\]>",
r" <Function test2\[2-a\]>",
r" <Function test1\[2-b\]>",
r" <Function test2\[2-b\]>",
]
)

def test_parametrize_multiple_times(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""
Expand Down