Skip to content

Commit 8c2a6c7

Browse files
Add tests and a little improvement
1 parent 565b624 commit 8c2a6c7

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

src/_pytest/python.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,38 +1175,35 @@ def get_direct_param_fixture_func(request: FixtureRequest) -> Any:
11751175
return request.param
11761176

11771177

1178-
def resolve_unique_values_and_their_indices_in_parametersets(
1178+
def resolve_values_indices_in_parametersets(
11791179
argnames: Sequence[str],
11801180
parametersets: Sequence[ParameterSet],
1181-
) -> Tuple[Dict[str, List[object]], List[Tuple[int]]]:
1182-
"""Resolve unique values and represent parameter sets by values' indices. The index of
1183-
a value in a parameter set is determined by where the value appears in the existing values
1184-
of the argname for the first time. For example, given ``argnames`` and ``parametersets``
1185-
below, the result would be:
1181+
) -> List[Tuple[int]]:
1182+
"""Resolve indices of the values in parameter sets. The index of a value is determined by
1183+
where the value first appears in the existing values of the argname. For example, given
1184+
``argnames`` and ``parametersets`` below, the result would be:
11861185
11871186
::
11881187
11891188
argnames = ["A", "B", "C"]
11901189
parametersets = [("a1", "b1", "c1"), ("a1", "b2", "c1"), ("a1", "b3", "c2")]
1191-
result[0] = {"A": ["a1"], "B": ["b1", "b2", "b3"], "C": ["c1", "c2"]}
1192-
result[1] = [(0, 0, 0), (0, 1, 0), (0, 2, 1)]
1190+
result = [(0, 0, 0), (0, 1, 0), (0, 2, 1)]
11931191
11941192
result is used in reordering tests to keep items using the same fixture close together.
11951193
11961194
:param argnames:
1197-
Argument names passed to ``parametrize()``.
1195+
Argument names passed to ``metafunc.parametrize()``.
11981196
:param parametersets:
11991197
The parameter sets, each containing a set of values corresponding
12001198
to ``argnames``.
12011199
:returns:
1202-
Tuple of unique parameter values and their indices in parametersets.
1200+
List of tuples of indices, each tuple for a parameter set.
12031201
"""
12041202
indices = []
12051203
argname_value_indices_for_hashable_ones: Dict[str, Dict[object, int]] = defaultdict(
12061204
dict
12071205
)
12081206
argvalues_count: Dict[str, int] = defaultdict(lambda: 0)
1209-
unique_values: Dict[str, List[object]] = defaultdict(list)
12101207
for i, argname in enumerate(argnames):
12111208
argname_indices = []
12121209
for parameterset in parametersets:
@@ -1221,13 +1218,11 @@ def resolve_unique_values_and_their_indices_in_parametersets(
12211218
] = argvalues_count[argname]
12221219
argname_indices.append(argvalues_count[argname])
12231220
argvalues_count[argname] += 1
1224-
unique_values[argname].append(value)
12251221
except TypeError: # `value` is not hashable
12261222
argname_indices.append(argvalues_count[argname])
12271223
argvalues_count[argname] += 1
1228-
unique_values[argname].append(value)
12291224
indices.append(argname_indices)
1230-
return unique_values, list(zip(*indices))
1225+
return list(zip(*indices))
12311226

12321227

12331228
# Used for storing artificial fixturedefs for direct parametrization.
@@ -1384,12 +1379,7 @@ def parametrize(
13841379
ids = self._resolve_parameter_set_ids(
13851380
argnames, ids, parametersets, nodeid=self.definition.nodeid
13861381
)
1387-
(
1388-
params_values,
1389-
param_indices_list,
1390-
) = resolve_unique_values_and_their_indices_in_parametersets(
1391-
argnames, parametersets
1392-
)
1382+
param_indices_list = resolve_values_indices_in_parametersets(argnames, parametersets)
13931383
# Store used (possibly generated) ids with parametrize Marks.
13941384
if _param_mark and _param_mark._param_ids_from and generated_ids is None:
13951385
object.__setattr__(_param_mark._param_ids_from, "_param_ids_generated", ids)
@@ -1427,7 +1417,7 @@ def parametrize(
14271417
argname=argname,
14281418
func=get_direct_param_fixture_func,
14291419
scope=scope_,
1430-
params=params_values[argname],
1420+
params=None,
14311421
unittest=False,
14321422
ids=None,
14331423
is_pseudo=True,

testing/python/fixtures.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import sys
33
import textwrap
44
from pathlib import Path
5-
from unittest.mock import Mock
65

76
import pytest
87
from _pytest import fixtures
@@ -13,7 +12,6 @@
1312
from _pytest.pytester import get_public_names
1413
from _pytest.pytester import Pytester
1514
from _pytest.python import Function
16-
from _pytest.scope import HIGH_SCOPES
1715

1816

1917
def test_getfuncargnames_functions():

testing/python/metafunc.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,31 @@ def test_foo(x):
15041504
"*= no tests collected, 1 error in *",
15051505
]
15061506
)
1507+
1508+
def test_parametrize_module_level_test_with_class_scope(self, pytester: Pytester) -> None:
1509+
pytester.makepyfile(
1510+
"""
1511+
import pytest
1512+
1513+
@pytest.fixture
1514+
def item(request):
1515+
return request._pyfuncitem
1516+
1517+
fixturedef = None
1518+
1519+
@pytest.mark.parametrize("x", [0, 1], scope="class")
1520+
def test_1(item, x):
1521+
global fixturedef
1522+
fixturedef = item._fixtureinfo.name2fixturedefs['x'][-1]
1523+
1524+
@pytest.mark.parametrize("x", [1, 2], scope="module")
1525+
def test_2(item, x):
1526+
global fixturedef
1527+
assert fixturedef == item._fixtureinfo.name2fixturedefs['x'][-1]
1528+
"""
1529+
)
1530+
result = pytester.runpytest()
1531+
assert result.ret == 0
15071532

15081533

15091534
class TestMetafuncFunctionalAuto:

0 commit comments

Comments
 (0)