|
2 | 2 |
|
3 | 3 | In general, using `help(symbol)` is the recommended way to get the latest documentation. In addition, this page provides an overview of the various elements in this package. |
4 | 4 |
|
5 | | -## 1 - Case functions |
| 5 | +## 1 - Fixtures |
| 6 | + |
| 7 | +### `current_cases` |
| 8 | + |
| 9 | +A fixture containing [`get_current_cases(request)`](#get_current_cases). |
| 10 | + |
| 11 | +This is a dictionary containing all case parameters for the currently active `pytest` item. For each test function argument parametrized using a [`@parametrize_with_case(<argname>, ...)`](#parametrize_with_cases) this dictionary contains an entry `{<argname>: (actual_id, case_function)}`. If several argnames are parametrized this way, a dedicated entry will be present for each argname. |
| 12 | + |
| 13 | +If a fixture parametrized with cases is active, the dictionary will contain an entry `{<fixturename>: <dct>}` where `<dct>` is a dictionary `{<argname>: (actual_id, case_function)}`. |
| 14 | + |
| 15 | +To get more information on a case function, you can use [`get_case_id(f)`](#get_case_id), [`get_case_marks(f)`](#get_case_marks), [`get_case_tags(f)`](#get_case_tags). You can also use [`matches_tag_query`](#matches_tag_query) to check if a case function matches some expectations either concerning its id |
| 16 | +or its tags. See [filters and tags documentation](https://smarie.github.io/python-pytest-cases/#filters-and-tags). |
| 17 | + |
| 18 | +## 2 - Case functions |
6 | 19 |
|
7 | 20 | As explained in the [documentation](index.md), case functions have no requirement anymore, and starting from version 2.0.0 of `pytest_cases` they can be parametrized with the usual `@pytest.mark.parametrize` or its improvement [`@parametrize`](#parametrize). Therefore the only remaining decorator is the optional `@case` decorator: |
8 | 21 |
|
@@ -233,7 +246,7 @@ CaseFilter(filter_function: Callable) |
233 | 246 |
|
234 | 247 | `CaseFilter` is the class used by all filters above, and implementing logical operations "and" (`&`) "or" (`|`) and "not" (`~`). You can use it to define a composable filter from any callable receiving a single `case` argument and returning a boolean indicating if the `case` is selected. |
235 | 248 |
|
236 | | -## 2 - Cases collection |
| 249 | +## 3 - Cases collection |
237 | 250 |
|
238 | 251 | ### `@parametrize_with_cases` |
239 | 252 |
|
@@ -282,27 +295,30 @@ argvalues = get_parametrize_args(host_class_or_module_of_f, cases_funs) |
282 | 295 |
|
283 | 296 | - `filter`: a callable receiving the case function and returning `True` or a truth value in case the function needs to be selected. |
284 | 297 |
|
285 | | - - `ids`: optional custom ids, similar to the one in `pytest.mark.parametrize`. Users may either provide an iterable of string ids, or a callable. If a callable is provided it will receive the case functions. Users may wish to use [`get_case_id`](#get_case_id) or other helpers in the [API](#1---case-functions) to inspect the case functions. |
| 298 | + - `ids`: optional custom ids, similar to the one in `pytest.mark.parametrize`. Users may either provide an iterable of string ids, or a callable. If a callable is provided it will receive the case functions. Users may wish to use [`get_case_id`](#get_case_id) or other helpers in the [API](#2-case-functions) to inspect the case functions. |
286 | 299 |
|
287 | 300 | - `idstyle`: This is mostly for debug. Style of ids to be used in the "union" fixtures generated by [`@parametrize`](#parametrize) if some cases are transformed into fixtures behind the scenes. `idstyle` possible values are `'compact'`, `'explicit'` or `None`/`'nostyle'` (default), or a callable. `idstyle` has no effect if no cases are transformed into fixtures. As opposed to `ids`, a callable provided here will receive a `ParamAlternative` object indicating which generated fixture should be used. See [`@parametrize`](#parametrize) for details. |
288 | 301 |
|
289 | 302 | - `scope`: The scope of the union fixture to create if `fixture_ref`s are found in the argvalues |
290 | 303 |
|
291 | 304 | - `import_fixtures`: experimental feature. Turn this to `True` in order to automatically import all fixtures defined in the cases module into the current module. |
292 | 305 |
|
293 | | -### `get_current_case_id` |
| 306 | +### `get_current_cases` |
294 | 307 |
|
295 | 308 | ```python |
296 | | -def get_current_case_id(request_or_item, |
297 | | - argnames: Union[Iterable[str], str] |
298 | | - ): |
| 309 | +def get_current_cases(request_or_item): |
299 | 310 | ``` |
300 | 311 |
|
301 | | -A helper function to return the current case id for a given `pytest` item (available in some hooks) or `request` |
302 | | -(available in hooks, and also directly as a fixture). |
| 312 | +Returns a dictionary containing all case parameters for the currently active `pytest` item. You can either pass the `pytest` item (available in some hooks) or the `request` (available in hooks, and also directly as a fixture). |
| 313 | + |
| 314 | +For each test function argument parametrized using a [`@parametrize_with_case(<argname>, ...)`](#parametrize_with_cases) this dictionary contains an entry `{<argname>: (actual_id, case_function)}`. If several argnames are parametrized this way, a dedicated entry will be present for each argname. |
| 315 | + |
| 316 | +If a fixture parametrized with cases is active, the dictionary will contain an entry `{<fixturename>: <dct>}` where `<dct>` is a dictionary `{<argname>: (actual_id, case_function)}`. |
| 317 | + |
| 318 | +To get more information on a case function, you can use [`get_case_id(f)`](#get_case_id), [`get_case_marks(f)`](#get_case_marks), [`get_case_tags(f)`](#get_case_tags). You can also use [`matches_tag_query`](#matches_tag_query) to check if a case function matches some expectations either concerning its id |
| 319 | +or its tags. See [filters and tags documentation](https://smarie.github.io/python-pytest-cases/#filters-and-tags). |
303 | 320 |
|
304 | | -You need to provide the argname(s) used in the corresponding `@parametrize_with_cases` so that this method finds |
305 | | -the right id. |
| 321 | +Note that you can get the same contents directly by using the [`current_cases`](#current_cases) fixture. |
306 | 322 |
|
307 | 323 | ### `get_all_cases` |
308 | 324 |
|
@@ -336,7 +352,7 @@ Transforms a list of cases (obtained from [`get_all_cases`](#get_all_cases)) int |
336 | 352 |
|
337 | 353 | - Otherwise, `case_fun` represents a single case: in that case a single `lazy_value` is returned. |
338 | 354 |
|
339 | | -## 3 - Pytest goodies |
| 355 | +## 4 - Pytest goodies |
340 | 356 |
|
341 | 357 | ### `@fixture` |
342 | 358 |
|
@@ -383,9 +399,9 @@ The created fixtures are automatically registered into the callers' module, but |
383 | 399 |
|
384 | 400 | ```python |
385 | 401 | import pytest |
386 | | -from pytest_cases import unpack_fixture, fixture_plus |
| 402 | +from pytest_cases import unpack_fixture, fixture |
387 | 403 |
|
388 | | -@fixture_plus |
| 404 | +@fixture |
389 | 405 | @pytest.mark.parametrize("o", ['hello', 'world']) |
390 | 406 | def c(o): |
391 | 407 | return o, o[0] |
|
0 commit comments