Python pytest.param() Examples
The following are 30
code examples of pytest.param().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pytest
, or try the search function
.
Example #1
Source File: fixture_parametrize_plus.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, valuegetter, # type: Callable[[], Any] id=None, # type: str marks=() # type: Sequence ): """ Creates a reference to a value getter, to be used in `parametrize_plus`. A `lazy_value` is the same thing than a function-scoped fixture, except that the value getter function is not a fixture and therefore can neither be parametrized nor depend on fixtures. It should have no mandatory argument. Note that a `lazy_value` can be included in a `pytest.param` without problem. In that case the id defined by `pytest.param` will take precedence over the one defined in `lazy_value` if any. The marks, however, will all be kept wherever they are defined. :param valuegetter: a callable without mandatory arguments :param id: an optional id. Otherwise `valuegetter.__name__` will be used by default :param marks: optional marks. `valuegetter` marks will also be preserved. """ self.valuegetter = valuegetter self._id = id self._marks = marks
Example #2
Source File: fixture_parametrize_plus.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 6 votes |
def explicit(param # type: ParamAlternative ): if isinstance(param, SingleParamAlternative): # return "%s_is_P%s" % (param.param_name, param.argvalues_index) return "%s_is_%s" % (param.argnames_str, param.get_id()) elif isinstance(param, MultiParamAlternative): return "%s_is_P%stoP%s" % (param.argnames_str, param.argvalues_index_from, param.argvalues_index_to - 1) elif isinstance(param, FixtureParamAlternative): return "%s_is_%s" % (param.argnames_str, param.alternative_name) elif isinstance(param, ProductParamAlternative): return "%s_is_P%s" % (param.argnames_str, param.argvalues_index) else: raise TypeError("Unsupported alternative: %r" % param) # @staticmethod # def compact(param): # return "U%s" % param.alternative_name
Example #3
Source File: test_context.py From pmdarima with MIT License | 6 votes |
def test_subsequent_contexts(): # Force a very fast fit with StepwiseContext(max_dur=.5), \ pytest.warns(UserWarning): auto_arima(lynx, stepwise=True) # Out of scope, should be EMPTY assert ContextStore.get_or_empty(ContextType.STEPWISE).get_type() \ is ContextType.EMPTY # Now show that we DON'T hit early termination by time here with StepwiseContext(max_steps=100), \ pytest.warns(UserWarning) as uw: ctx = ContextStore.get_or_empty(ContextType.STEPWISE) assert ctx.get_type() is ContextType.STEPWISE assert ctx.max_dur is None auto_arima(lynx, stepwise=True) # assert that max_dur was NOT reached assert not any(str(w.message) .startswith('early termination') for w in uw) # test param validation of ContextStore's add, get and remove members
Example #4
Source File: integration_test_utils.py From aws-encryption-sdk-python with Apache License 2.0 | 6 votes |
def all_test_vectors() -> Iterable[Any]: """Collect and iterate through all test vectors.""" with open(test_vectors_filename(), "r") as vectors_file: raw_vectors = json.load(vectors_file) for vector in raw_vectors: vector_name = "::".join([vector["key-type"], vector["algorithm-suite"]]) plaintext = base64.b64decode(vector["plaintext"].encode("utf-8")) ciphertext = base64.b64decode(vector["ciphertext"].encode("utf-8")) yield pytest.param( TestVector( plaintext=plaintext, ciphertext=ciphertext, key_type=vector["key-type"], algorithm_suite=vector["algorithm-suite"], ), id=vector_name, )
Example #5
Source File: fixture_core1_unions.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 6 votes |
def is_fixture_union_params(params): """ Internal helper to quickly check if a bunch of parameters correspond to a union fixture. :param params: :return: """ try: if len(params) < 1: return False else: if getattr(params, '__module__', '').startswith('pytest_cases'): # a value_ref_tuple or another proxy object created somewhere in our code, not a list return False p0 = params[0] if is_marked_parameter_value(p0): p0 = get_marked_parameter_values(p0)[0] return isinstance(p0, UnionFixtureAlternative) except TypeError: raise InvalidParamsList(params)
Example #6
Source File: test_tools.py From recruit with Apache License 2.0 | 6 votes |
def epochs(epoch_1960, request): """Timestamp at 1960-01-01 in various forms. * pd.Timestamp * datetime.datetime * numpy.datetime64 * str """ assert request.param in {'timestamp', 'pydatetime', 'datetime64', "str_1960"} if request.param == 'timestamp': return epoch_1960 elif request.param == 'pydatetime': return epoch_1960.to_pydatetime() elif request.param == "datetime64": return epoch_1960.to_datetime64() else: return str(epoch_1960)
Example #7
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_pytest_marks_on_function(f, as_decorators=False): """ Utility to return *ALL* pytest marks (not only parametrization) applied on a function :param f: :param as_decorators: transforms the marks into decorators before returning them :return: """ try: mks = f.pytestmark except AttributeError: try: # old pytest < 3: marks are set as fields on the function object # but they do not have a particulat type, their type is 'instance'... mks = [v for v in vars(f).values() if str(v).startswith("<MarkInfo '")] except AttributeError: mks = [] if as_decorators: return transform_marks_into_decorators(mks) else: return mks
Example #8
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_param_names(fnode): """ Returns a list of parameter names for the given pytest Function node. parameterization marks containing several names are split :param fnode: :return: """ p_markers = get_parametrization_markers(fnode) param_names = [] for paramz_mark in p_markers: param_names += get_param_argnames_as_list(paramz_mark.args[0]) return param_names # ---------------- working on functions
Example #9
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_marked_parameter_id(v): return v.kwargs.get('id', None) # ---- tools to reapply marks on test parameter values, whatever the pytest version ---- # Compatibility for the way we put marks on single parameters in the list passed to @pytest.mark.parametrize # see https://docs.pytest.org/en/3.3.0/skipping.html?highlight=mark%20parametrize#skip-xfail-with-parametrize # check if pytest.param exists
Example #10
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def analyze_parameter_set(pmark=None, argnames=None, argvalues=None, ids=None, check_nb=True): """ analyzes a parameter set passed either as a pmark or as distinct (argnames, argvalues, ids) to extract/construct the various ids, marks, and values See also pytest.Metafunc.parametrize method, that calls in particular pytest.ParameterSet._for_parametrize and _pytest.python._idvalset :param pmark: :param argnames: :param argvalues: :param ids: :param check_nb: a bool indicating if we should raise an error if len(argnames) > 1 and any argvalue has a different length than len(argnames) :return: ids, marks, values """ if pmark is not None: if any(a is not None for a in (argnames, argvalues, ids)): raise ValueError("Either provide a pmark OR the details") argnames = pmark.param_names argvalues = pmark.param_values ids = pmark.param_ids # extract all parameters that have a specific configuration (pytest.param()) custom_pids, p_marks, p_values = extract_parameterset_info(argnames, argvalues, check_nb=check_nb) # get the ids by merging/creating the various possibilities p_ids = make_test_ids(argnames=argnames, argvalues=p_values, global_ids=ids, id_marks=custom_pids) return p_ids, p_marks, p_values
Example #11
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def extract_parameterset_info(argnames, argvalues, check_nb=True): """ :param argnames: the names in this parameterset :param argvalues: the values in this parameterset :param check_nb: a bool indicating if we should raise an error if len(argnames) > 1 and any argvalue has a different length than len(argnames) :return: """ pids = [] pmarks = [] pvalues = [] if isinstance(argnames, string_types): raise TypeError("argnames must be an iterable. Found %r" % argnames) nbnames = len(argnames) for v in argvalues: # is this a pytest.param() ? if is_marked_parameter_value(v): # --id _id = get_marked_parameter_id(v) pids.append(_id) # --marks marks = get_marked_parameter_marks(v) pmarks.append(marks) # note: there might be several # --value(a tuple if this is a tuple parameter) v = get_marked_parameter_values(v) if nbnames == 1: pvalues.append(v[0]) else: pvalues.append(v) else: # normal argvalue pids.append(None) pmarks.append(None) pvalues.append(v) if check_nb and nbnames > 1 and (len(v) != nbnames): raise ValueError("Inconsistent number of values in pytest parametrize: %s items found while the " "number of parameters is %s: %s." % (len(v), nbnames, v)) return pids, pmarks, pvalues
Example #12
Source File: test_value_exprs.py From ibis with Apache License 2.0 | 5 votes |
def log(request): return operator.methodcaller(request.param)
Example #13
Source File: utils.py From m2cgen with MIT License | 5 votes |
def cartesian_e2e_params(executors_with_marks, models_with_trainers_with_marks, *additional_params): result_params = list(additional_params) # Specifying None for additional parameters makes pytest to generate # automatic ids. If we don't do this pytest will throw exception that # number of parameters doesn't match number of provided ids ids = [None] * len(additional_params) prod = itertools.product( executors_with_marks, models_with_trainers_with_marks) for (executor, executor_mark), (model, trainer, trainer_mark) in prod: # Since we reuse the same model across multiple tests we want it # to be clean. model = clone(model) # We use custom id since pytest for some reason can't show name of # the model in the automatic id. Which sucks. ids.append(f"{_get_full_model_name(model)} - " f"{executor_mark.name} - {trainer.name}") result_params.append(pytest.param( model, executor, trainer, marks=[executor_mark, trainer_mark], )) param_names = "estimator,executor_cls,model_trainer" def wrap(func): @pytest.mark.parametrize(param_names, result_params, ids=ids) @functools.wraps(func) def inner(*args, **kwarg): return func(*args, **kwarg) return inner return wrap
Example #14
Source File: test_working_set.py From pkg_resources with MIT License | 5 votes |
def parametrize_test_working_set_resolve(*test_list): idlist = [] argvalues = [] for test in test_list: ( name, installed_dists, installable_dists, requirements, expected1, expected2 ) = [ strip_comments(s.lstrip()) for s in textwrap.dedent(test).lstrip().split('\n\n', 5) ] installed_dists = list(parse_distributions(installed_dists)) installable_dists = list(parse_distributions(installable_dists)) requirements = list(pkg_resources.parse_requirements(requirements)) for id_, replace_conflicting, expected in ( (name, False, expected1), (name + '_replace_conflicting', True, expected2), ): idlist.append(id_) expected = strip_comments(expected.strip()) if re.match('\w+$', expected): expected = getattr(pkg_resources, expected) assert issubclass(expected, Exception) else: expected = list(parse_distributions(expected)) argvalues.append(pytest.param(installed_dists, installable_dists, requirements, replace_conflicting, expected)) return pytest.mark.parametrize('installed_dists,installable_dists,' 'requirements,replace_conflicting,' 'resolved_dists_or_exception', argvalues, ids=idlist)
Example #15
Source File: fontinfo_test.py From glyphsLib with Apache License 2.0 | 5 votes |
def section(name, *fields): return pytest.param(fields, id=name)
Example #16
Source File: fontinfo_test.py From glyphsLib with Apache License 2.0 | 5 votes |
def skip_section(name, *fields): return pytest.param(fields, id=name, marks=pytest.mark.skip)
Example #17
Source File: testing.py From funsor with Apache License 2.0 | 5 votes |
def xfail_param(*args, **kwargs): return pytest.param(*args, marks=[pytest.mark.xfail(**kwargs)])
Example #18
Source File: test_operations.py From ibis with Apache License 2.0 | 5 votes |
def test_scalar_parameter(t, df, raw_value): value = ibis.param(dt.double) expr = t.float64_with_zeros == value result = expr.execute(params={value: raw_value}) expected = df.float64_with_zeros == raw_value tm.assert_series_equal(result, expected)
Example #19
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def transform_marks_into_decorators(marks): """ Transforms the provided marks (MarkInfo) obtained from marked cases, into MarkDecorator so that they can be re-applied to generated pytest parameters in the global @pytest.mark.parametrize. :param marks: :return: """ marks_mod = [] try: # suppress the warning message that pytest generates when calling pytest.mark.MarkDecorator() directly with warnings.catch_warnings(): warnings.simplefilter("ignore") for m in marks: md = pytest.mark.MarkDecorator() if LooseVersion(pytest.__version__) >= LooseVersion('3.0.0'): if isinstance(m, type(md)): # already a decorator, we can use it marks_mod.append(m) else: md.mark = m marks_mod.append(md) else: # always recreate one, type comparison does not work (all generic stuff) md.name = m.name # md.markname = m.name md.args = m.args md.kwargs = m.kwargs # markinfodecorator = getattr(pytest.mark, markinfo.name) # markinfodecorator(*markinfo.args) marks_mod.append(md) except Exception as e: warn("Caught exception while trying to mark case: [%s] %s" % (type(e), e)) return marks_mod
Example #20
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def make_test_ids_from_param_values(param_names, param_values, ): """ Replicates pytest behaviour to generate the ids when there are several parameters in a single `parametrize. Note that param_values should not contain marks. :param param_names: :param param_values: :return: a list of param ids """ if isinstance(param_names, string_types): raise TypeError("param_names must be an iterable. Found %r" % param_names) nb_params = len(param_names) if nb_params == 0: raise ValueError("empty list provided") elif nb_params == 1: paramids = [] for _idx, v in enumerate(param_values): _id = mini_idvalset(param_names, (v,), _idx) paramids.append(_id) else: paramids = [] for _idx, vv in enumerate(param_values): if len(vv) != nb_params: raise ValueError("Inconsistent lenghts for parameter names and values: '%s' and '%s'" "" % (param_names, vv)) _id = mini_idvalset(param_names, vv, _idx) paramids.append(_id) return paramids # ---- ParameterSet api ---
Example #21
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def make_test_ids(global_ids, id_marks, argnames=None, argvalues=None, precomputed_ids=None): """ Creates the proper id for each test based on (higher precedence first) - any specific id mark from a `pytest.param` (`id_marks`) - the global `ids` argument of pytest parametrize (`global_ids`) - the name and value of parameters (`argnames`, `argvalues`) or the precomputed ids(`precomputed_ids`) See also _pytest.python._idvalset method :param global_ids: :param id_marks: :param argnames: :param argvalues: :param precomputed_ids: :return: """ if global_ids is not None: # overridden at global pytest.mark.parametrize level - this takes precedence. try: # an explicit list of ids ? p_ids = list(global_ids) except TypeError: # a callable to apply on the values p_ids = list(global_ids(v) for v in argvalues) else: # default: values-based if precomputed_ids is not None: if argnames is not None or argvalues is not None: raise ValueError("Only one of `precomputed_ids` or argnames/argvalues should be provided.") p_ids = precomputed_ids else: p_ids = make_test_ids_from_param_values(argnames, argvalues) # Finally, local pytest.param takes precedence over everything else for i, _id in enumerate(id_marks): if _id is not None: p_ids[i] = _id return p_ids
Example #22
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def combine_ids(paramid_tuples): """ Receives a list of tuples containing ids for each parameterset. Returns the final ids, that are obtained by joining the various param ids by '-' for each test node :param paramid_tuples: :return: """ # return ['-'.join(pid for pid in testid) for testid in paramid_tuples]
Example #23
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_parametrization_markers(fnode): """ Returns the parametrization marks on a pytest Function node. :param fnode: :return: """ if LooseVersion(pytest.__version__) >= LooseVersion('3.4.0'): return list(fnode.iter_markers(name="parametrize")) else: return list(fnode.parametrize)
Example #24
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_param_argnames_as_list(argnames): """ pytest parametrize accepts both coma-separated names and list/tuples. This function makes sure that we always return a list :param argnames: :return: """ if isinstance(argnames, string_types): argnames = argnames.replace(' ', '').split(',') return list(argnames) # ------------ container for the mark information that we grab from the fixtures (`@fixture_plus`)
Example #25
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_fixture_scope(fixture_fun): """ Internal utility to retrieve the fixture scope corresponding to the given fixture function . Indeed there is currently no pytest API to do this. :param fixture_fun: :return: """ assert_is_fixture(fixture_fun) return fixture_fun._pytestfixturefunction.scope # noqa # except AttributeError: # # pytest 2 # return fixture_fun.func_scope
Example #26
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def assert_is_fixture(fixture_fun # type: Any ): """ Raises a ValueError if the provided fixture function is not a fixture. :param fixture_fun: :return: """ if not is_fixture(fixture_fun): raise ValueError("The provided fixture function does not seem to be a fixture: %s. Did you properly decorate " "it ?" % fixture_fun)
Example #27
Source File: common_pytest.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def is_fixture(fixture_fun # type: Any ): """ Returns True if the provided function is a fixture :param fixture_fun: :return: """ try: fixture_fun._pytestfixturefunction # noqa return True except AttributeError: # not a fixture ? return False
Example #28
Source File: test_fixtures_paramfixtures.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def myfix(arg1, arg2, parg1): """One parameterized fixture relying on above param fixture""" return arg1, arg2, parg1
Example #29
Source File: test_fixtures_parametrize.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_synthesis(module_results_dct): """Use pytest-harvest to check that the list of executed tests is correct """ assert list(module_results_dct) == ['test_one[one-one]', 'test_one[one-two]', 'test_one[two-one]', 'test_one[two-two]'] # pytest.param - not available in all versions
Example #30
Source File: test_lazy_value.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_synthesis(module_results_dct): assert list(module_results_dct) == ['test_foo_single[val]', 'test_foo_single[A]', 'test_foo_multi[a0-b0]', # normal: lazy_value is used for the whole tuple # AND we cannot use pytest.param in this version # AND there are no fixtures so we pass to normal @parametrize 'test_foo_multi[1-val]']