refactor resolve_arg_value_types

* more explicit type checks
* expand from list+tuple to sequence
This commit is contained in:
Ronny Pfannschmidt 2019-07-12 17:47:52 +02:00
parent 898028cb22
commit 2c071a060e
2 changed files with 24 additions and 8 deletions

View File

@ -1,11 +1,12 @@
""" Python test discovery, setup and run of test functions. """ """ Python test discovery, setup and run of test functions. """
import collections
import enum import enum
import fnmatch import fnmatch
import inspect import inspect
import os import os
import sys import sys
import warnings import warnings
from collections import Counter
from collections.abc import Sequence
from functools import partial from functools import partial
from textwrap import dedent from textwrap import dedent
@ -1042,12 +1043,9 @@ class Metafunc(fixtures.FuncargnamesCompatAttr):
* "params" if the argname should be the parameter of a fixture of the same name. * "params" if the argname should be the parameter of a fixture of the same name.
* "funcargs" if the argname should be a parameter to the parametrized test function. * "funcargs" if the argname should be a parameter to the parametrized test function.
""" """
valtypes = {} if isinstance(indirect, bool):
if indirect is True: valtypes = dict.fromkeys(argnames, "params" if indirect else "funcargs")
valtypes = dict.fromkeys(argnames, "params") elif isinstance(indirect, Sequence):
elif indirect is False:
valtypes = dict.fromkeys(argnames, "funcargs")
elif isinstance(indirect, (tuple, list)):
valtypes = dict.fromkeys(argnames, "funcargs") valtypes = dict.fromkeys(argnames, "funcargs")
for arg in indirect: for arg in indirect:
if arg not in argnames: if arg not in argnames:
@ -1058,6 +1056,13 @@ class Metafunc(fixtures.FuncargnamesCompatAttr):
pytrace=False, pytrace=False,
) )
valtypes[arg] = "params" valtypes[arg] = "params"
else:
fail(
"In {func}: expected Sequence or boolean for indirect, got {type}".format(
type=type(indirect).__name__, func=self.function.__name__
),
pytrace=False,
)
return valtypes return valtypes
def _validate_if_using_arg_names(self, argnames, indirect): def _validate_if_using_arg_names(self, argnames, indirect):
@ -1185,7 +1190,7 @@ def idmaker(argnames, parametersets, idfn=None, ids=None, config=None, item=None
if len(set(ids)) != len(ids): if len(set(ids)) != len(ids):
# The ids are not unique # The ids are not unique
duplicates = [testid for testid in ids if ids.count(testid) > 1] duplicates = [testid for testid in ids if ids.count(testid) > 1]
counters = collections.defaultdict(lambda: 0) counters = Counter()
for index, testid in enumerate(ids): for index, testid in enumerate(ids):
if testid in duplicates: if testid in duplicates:
ids[index] = testid + str(counters[testid]) ids[index] = testid + str(counters[testid])

View File

@ -599,6 +599,17 @@ class TestMetafunc:
assert metafunc._calls[0].funcargs == dict(x="a", y="b") assert metafunc._calls[0].funcargs == dict(x="a", y="b")
assert metafunc._calls[0].params == {} assert metafunc._calls[0].params == {}
def test_parametrize_indirect_wrong_type(self):
def func(x, y):
pass
metafunc = self.Metafunc(func)
with pytest.raises(
pytest.fail.Exception,
match="In func: expected Sequence or boolean for indirect, got dict",
):
metafunc.parametrize("x, y", [("a", "b")], indirect={})
def test_parametrize_indirect_list_functional(self, testdir): def test_parametrize_indirect_list_functional(self, testdir):
""" """
#714 #714