Skip to content

Commit 01180ee

Browse files
committed
Shrinking parametrize_callspecs by using Metafunc.parametrize
1 parent 46c2842 commit 01180ee

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

pytest_lazyfixture.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
2+
import copy
23
import sys
34
import types
45
from collections import defaultdict
56
import pytest
6-
from _pytest.fixtures import scopenum_function
77

88

99
PY3 = sys.version_info[0] == 3
@@ -79,30 +79,21 @@ def normalize_metafunc_calls(metafunc, valtype):
7979
metafunc._calls = newcalls
8080

8181

82+
def copy_metafunc(metafunc):
83+
copied = copy.copy(metafunc)
84+
copied.fixturenames = copy.copy(metafunc.fixturenames)
85+
copied._calls = []
86+
copied._ids = copy.copy(metafunc._ids)
87+
copied._arg2fixturedefs = copy.copy(metafunc._arg2fixturedefs)
88+
return copied
89+
90+
8291
def parametrize_callspecs(callspecs, metafunc, fname, fparams):
83-
allnewcallspecs = []
84-
for i, param in enumerate(fparams):
85-
try:
86-
newcallspecs = [call.copy() for call in callspecs]
87-
except TypeError:
88-
# pytest < 3.6.3
89-
newcallspecs = [call.copy(metafunc) for call in callspecs]
90-
91-
# TODO: for now it uses only function scope
92-
# TODO: idlist
93-
setmulti_args = (
94-
{fname: 'params'}, (fname,), (param,),
95-
None, (), scopenum_function, i
96-
)
97-
try:
98-
for newcallspec in newcallspecs:
99-
newcallspec.setmulti2(*setmulti_args)
100-
except AttributeError:
101-
# pytest < 3.3.0
102-
for newcallspec in newcallspecs:
103-
newcallspec.setmulti(*setmulti_args)
104-
allnewcallspecs.extend(newcallspecs)
105-
return allnewcallspecs
92+
newmetafunc = copy_metafunc(metafunc)
93+
newmetafunc._calls = callspecs
94+
newmetafunc.fixturenames.append(fname)
95+
newmetafunc.parametrize(fname, fparams, indirect=True)
96+
return newmetafunc._calls
10697

10798

10899
def normalize_call(callspec, metafunc, valtype, used_keys=None):

0 commit comments

Comments
 (0)