Skip to content

Commit af6665c

Browse files
author
Sylvain MARIE
committed
@parametrize_with_cases now explicitly supports all id customization methods (ids, idgen and idstyle) supported by @parametrize (ids, idgen and idstyle). Updated documentation accordingly. Fixed #151
1 parent 26df3cf commit af6665c

File tree

5 files changed

+250
-59
lines changed

5 files changed

+250
-59
lines changed

docs/api_reference.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def case_hi():
4343
glob:str = None,
4444
has_tag: Union[str, Iterable[str]]=None,
4545
filter: Callable = None,
46+
ids: Union[Callable, Iterable[str]]=None,
47+
idstyle: Optional[str]='explicit',
48+
idgen: Union[str, Callable]=_IDGEN,
4649
**kwargs
4750
)
4851
```
@@ -77,7 +80,12 @@ argvalues = get_parametrize_args(host_class_or_module_of_f, cases_funs)
7780
- `has_tag`: a single tag or a tuple, set, list of tags that should be matched by the ones set with the [`@case`](#case) decorator on the case function(s) to be selected.
7881

7982
- `filter`: a callable receiving the case function and returning True or a truth value in case the function needs to be selected.
80-
83+
84+
- `ids`: same as in pytest.mark.parametrize. Note that an alternative way to create ids exists with `idgen`. Only one non-None `ids` or `idgen` should be provided. See [`@parametrize`](#parametrize) for details.
85+
86+
- `idgen`: an id formatter. Either a string representing a template, or a callable receiving all argvalues at once (as opposed to the behaviour in pytest ids). This alternative way to generate ids can only be used when `ids` is not provided (None). You can use the special `pytest_cases.AUTO` formatter to generate an automatic id with template `<name>=<value>-<name2>=<value2>-...`. See [`@parametrize`](#parametrize) for details.
87+
88+
- `idstyle`: style of ids to be used in the "union" fixtures generated by [`@parametrize`](#parametrize) when some cases require fixtures. One of 'compact', 'explicit' or None/'nostyle'. See [`@parametrize`](#parametrize) for details.
8189

8290
### `get_all_cases`
8391

@@ -272,16 +280,16 @@ Identical to `param_fixtures` but for a single parameter name, so that you can a
272280
### `@parametrize`
273281

274282
```python
275-
@parametrize_plus(argnames: str=None,
276-
argvalues: Iterable[Any]=None,
277-
indirect: bool = False,
278-
ids: Union[Callable, Iterable[str]] = None,
279-
idstyle: str = 'explicit',
280-
idgen: Union[str, Callable] = _IDGEN,
281-
scope: str = None,
282-
hook: Callable = None,
283-
debug: bool = False,
284-
**args)
283+
@parametrize(argnames: str=None,
284+
argvalues: Iterable[Any]=None,
285+
indirect: bool = False,
286+
ids: Union[Callable, Iterable[str]] = None,
287+
idstyle: Optional[str] = 'explicit',
288+
idgen: Union[str, Callable] = _IDGEN,
289+
scope: str = None,
290+
hook: Callable = None,
291+
debug: bool = False,
292+
**args)
285293
```
286294

287295
Equivalent to `@pytest.mark.parametrize` but also supports
@@ -302,6 +310,29 @@ Finally, `pytest.param` is supported even when there are `fixture_ref` and `lazy
302310

303311
Here as for all functions above, an optional `hook` can be passed, to apply on each fixture function that is created during this call. The hook function will be called everytime a fixture is about to be created. It will receive a single argument (the function implementing the fixture) and should return the function to use. For example you can use `saved_fixture` from `pytest-harvest` as a hook in order to save all such created fixtures in the fixture store.
304312

313+
**Parameters**
314+
315+
- `argnames`: same than in `@pytest.mark.parametrize`
316+
317+
- `argvalues: same as in pytest.mark.parametrize except that `fixture_ref` and `lazy_value` are supported
318+
319+
- `indirect`: same as in pytest.mark.parametrize. Note that it is not recommended and is not guaranteed to work in complex parametrization scenarii.
320+
321+
- `ids`: same as in pytest.mark.parametrize. Note that an alternative way to create ids exists with `idgen`. Only one non-None `ids` or `idgen` should be provided.
322+
323+
- `idgen`: an id formatter. Either a string representing a template, or a callable receiving all argvalues at once (as opposed to the behaviour in pytest ids). This alternative way to generate ids can only be used when `ids` is not provided (None). You can use the special `pytest_cases.AUTO` formatter to generate an automatic id with template `<name>=<value>-<name2>=<value2>-...`.
324+
325+
- `idstyle`: style of ids to be used in the "union" fixtures generated by `@parametrize` when some cases require fixtures. One of 'compact', 'explicit' or None/'nostyle'.
326+
327+
- `scope`: same as in pytest.mark.parametrize
328+
329+
- `hook`: an optional hook to apply to each fixture function that is created during this call. The hook function will be called everytime a fixture is about to be created. It will receive a single argument (the function implementing the fixture) and should return the function to use. For example you can use `saved_fixture` from `pytest-harvest` as a hook in order to save all such created fixtures in the fixture store.
330+
331+
- `debug`: print debug messages on stdout to analyze fixture creation (use pytest -s to see them)
332+
333+
- `args`: additional {argnames: argvalues} definition
334+
335+
305336
### `lazy_value`
306337

307338
```python

pytest_cases/case_parametrizer_new.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@
2727
from . import fixture
2828
from .case_funcs_new import matches_tag_query, is_case_function, is_case_class, CaseInfo, CASE_PREFIX_FUN
2929
from .fixture__creation import check_name_available, CHANGE
30-
from .fixture_parametrize_plus import fixture_ref, _parametrize_plus
31-
30+
from .fixture_parametrize_plus import fixture_ref, _parametrize_plus, _IDGEN
3231

3332
THIS_MODULE = object()
3433
"""Singleton that can be used instead of a module name to indicate that the module is the current one"""
3534

3635
try:
37-
from typing import Literal # noqa
36+
from typing import Literal, Optional # noqa
3837
from types import ModuleType # noqa
3938

4039
ModuleRef = Union[str, ModuleType, Literal[AUTO], Literal[AUTO2], Literal[THIS_MODULE]] # noqa
@@ -49,6 +48,9 @@ def parametrize_with_cases(argnames, # type: Union[str, List[str]
4948
glob=None, # type: str
5049
has_tag=None, # type: Any
5150
filter=None, # type: Callable[[Callable], bool] # noqa
51+
ids=None, # type: Union[Callable, Iterable[str]]
52+
idstyle='explicit', # type: Optional[str]
53+
idgen=_IDGEN, # type: Union[str, Callable]
5254
debug=False, # type: bool
5355
**kwargs
5456
):
@@ -95,6 +97,14 @@ def parametrize_with_cases(argnames, # type: Union[str, List[str]
9597
decorator on the case function(s) to be selected.
9698
:param filter: a callable receiving the case function and returning True or a truth value in case the function
9799
needs to be selected.
100+
:param ids: same as in pytest.mark.parametrize. Note that an alternative way to create ids exists with `idgen`. Only
101+
one non-None `ids` or `idgen` should be provided. See `@parametrize` for details.
102+
:param idgen: an id formatter. Either a string representing a template, or a callable receiving all argvalues
103+
at once (as opposed to the behaviour in pytest ids). This alternative way to generate ids can only be used when
104+
`ids` is not provided (None). You can use the special `AUTO` formatter to generate an automatic id with
105+
template <name>=<value>-<name2>=<value2>-etc. See `@parametrize` for details.
106+
:param idstyle: style of ids to be used in the "union" fixtures generated by `@parametrize` when some cases require
107+
fixtures. One of 'compact', 'explicit' or None/'nostyle'. See `@parametrize` for details.
98108
:param debug: a boolean flag to debug what happens behind the scenes
99109
:return:
100110
"""
@@ -112,7 +122,8 @@ def _apply_parametrization(f, host_class_or_module):
112122

113123
# Finally apply parametrization - note that we need to call the private method so that fixture are created in
114124
# the right module (not here)
115-
_parametrize_with_cases, needs_inject = _parametrize_plus(argnames, argvalues, debug=debug, **kwargs)
125+
_parametrize_with_cases, needs_inject = _parametrize_plus(argnames, argvalues, ids=ids, idstyle=idstyle,
126+
idgen=idgen, debug=debug, **kwargs)
116127

117128
if needs_inject:
118129
return _parametrize_with_cases(f, host_class_or_module)

pytest_cases/fixture_core1_unions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def _fixture_union(fixtures_dest,
291291
fix_alternatives, # type: Sequence[UnionFixtureAlternative]
292292
unique_fix_alt_names, # type: List[str]
293293
scope="function", # type: str
294-
idstyle="explicit", # type: str
294+
idstyle="explicit", # type: Optional[str]
295295
ids=None, # type: Union[Callable, Iterable[str]]
296296
autouse=False, # type: bool
297297
hook=None, # type: Callable[[Callable], Callable]

0 commit comments

Comments
 (0)