Skip to content

Commit ed4929d

Browse files
author
Sylvain MARIE
committed
Fixed documentation for the new fixture
1 parent 7d389ab commit ed4929d

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

docs/api_reference.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22

33
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.
44

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
619

720
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:
821

@@ -233,7 +246,7 @@ CaseFilter(filter_function: Callable)
233246

234247
`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.
235248

236-
## 2 - Cases collection
249+
## 3 - Cases collection
237250

238251
### `@parametrize_with_cases`
239252

@@ -282,27 +295,30 @@ argvalues = get_parametrize_args(host_class_or_module_of_f, cases_funs)
282295

283296
- `filter`: a callable receiving the case function and returning `True` or a truth value in case the function needs to be selected.
284297

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.
286299

287300
- `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.
288301

289302
- `scope`: The scope of the union fixture to create if `fixture_ref`s are found in the argvalues
290303

291304
- `import_fixtures`: experimental feature. Turn this to `True` in order to automatically import all fixtures defined in the cases module into the current module.
292305

293-
### `get_current_case_id`
306+
### `get_current_cases`
294307

295308
```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):
299310
```
300311

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).
303320

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.
306322

307323
### `get_all_cases`
308324

@@ -336,7 +352,7 @@ Transforms a list of cases (obtained from [`get_all_cases`](#get_all_cases)) int
336352

337353
- Otherwise, `case_fun` represents a single case: in that case a single `lazy_value` is returned.
338354

339-
## 3 - Pytest goodies
355+
## 4 - Pytest goodies
340356

341357
### `@fixture`
342358

@@ -383,9 +399,9 @@ The created fixtures are automatically registered into the callers' module, but
383399

384400
```python
385401
import pytest
386-
from pytest_cases import unpack_fixture, fixture_plus
402+
from pytest_cases import unpack_fixture, fixture
387403

388-
@fixture_plus
404+
@fixture
389405
@pytest.mark.parametrize("o", ['hello', 'world'])
390406
def c(o):
391407
return o, o[0]

docs/index.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
[![Documentation](https://img.shields.io/badge/doc-latest-blue.svg)](https://smarie.github.io/python-pytest-cases/) [![PyPI](https://img.shields.io/pypi/v/pytest-cases.svg)](https://pypi.python.org/pypi/pytest-cases/) [![Downloads](https://pepy.tech/badge/pytest-cases)](https://pepy.tech/project/pytest-cases) [![Downloads per week](https://pepy.tech/badge/pytest-cases/week)](https://pepy.tech/project/pytest-cases) [![GitHub stars](https://img.shields.io/github/stars/smarie/python-pytest-cases.svg)](https://github.com/smarie/python-pytest-cases/stargazers)
88
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.3937829.svg)](https://doi.org/10.5281/zenodo.3937829)
99

10+
!!! success "New `current_cases` fixture to easily know the current case for each parameter ! See [below](#c-accessing-the-current-case) for details."
11+
1012
!!! success "Major refactoring of test ids in v3.0.0 ! See [below](#d-test-ids) for details."
1113

1214
!!! success "`@parametrize` now automatically detects fixture symbols ! See [documentation](./pytest_goodies.md#parametrize) for details."
@@ -475,22 +477,50 @@ def test_caching(cached_a, d):
475477
476478
!!! warning "If you add a cache mechanism, make sure that your test functions do not modify the returned objects !"
477479
478-
### c- Accessing the current case id
480+
### c- Accessing the current case
481+
482+
In some scenarii you may wish to access the case functions that are currently used to provide the parameter values. This may be
483+
484+
- to make your test behave differently depending on the case function, case id or case tags
485+
- to `pytest.skip` some combinations of parameters/cases that do not make sense
486+
- ...
479487
480-
You may need to access the current case id from within a `pytest` hook or the test itself. For this the `get_current_case_id` helper function is provided:
488+
With `pytest-cases` starting in version `3.5`, this is now possible thanks to the `current_cases` fixture. Simply use this fixture to get a dictionary containing the actual parameter id and case function for all parameters parametrized with cases in the current test node. Parametrized fixtures, if any, will appear in a sub-dictionary indexed by the fixture name.
481489
482490
```python
483-
from pytest_cases import parametrize_with_cases, get_current_case_id
491+
from pytest_cases import parametrize_with_cases, fixture
484492
485493
def case_a():
486494
return 1
487495
496+
@fixture
497+
@parametrize_with_cases("foo", cases=case_a)
498+
def my_fixture(foo):
499+
return foo
500+
488501
@parametrize_with_cases("data", cases=case_a)
489-
def test_lazy_val_case(data, request):
490-
assert get_current_case_id(request, "data") == "a"
502+
def test_get_current_case(data, my_fixture, current_cases):
503+
504+
# this is how to access the case function for a test parameter
505+
actual_case_id, case_fun = current_cases["data"]
506+
507+
# this is how to access the case function for a fixture parameter
508+
fix_actual_case_id, fix_case_fun = current_cases["my_fixture"]["foo"]
509+
510+
# let's print everything
511+
print(current_cases)
491512
```
492513
493-
See [API reference](./api_reference.md#get_current_case_id) for details.
514+
yields
515+
516+
```
517+
{'data': ('a', <function case_a at 0x00000205BED1CF28>),
518+
'my_fixture': {'foo': ('a', <function case_a at 0x00000205BED1CF28>)}}
519+
```
520+
521+
To get more information on the case function, you can use `get_case_id(f)`, `get_case_marks(f)`, `get_case_tags(f)`. You can also use `matches_tag_query` to check if a case function matches some expectations either concerning its id or its tags. See [API reference](./api_reference.md#matches_tag_query).
522+
523+
Note: you can get the same information from a pytest hook, using the `get_current_cases` function. See [API reference](./api_reference.md#get_current_cases) for details.
494524
495525
### d- Test ids
496526

pytest_cases/plugin.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,10 +1468,7 @@ def current_cases(request):
14681468
"""
14691469
A fixture containing `get_current_cases(request)`
14701470
1471-
Returns a dictionary containing all case parameters for the currently active `pytest` item.
1472-
You can either pass the `pytest` item (available in some hooks) or the `request` (available in hooks, and also
1473-
directly as a fixture).
1474-
1471+
This is a dictionary containing all case parameters for the currently active `pytest` item.
14751472
For each test function argument parametrized using a `@parametrize_with_case(<argname>, ...)` this dictionary
14761473
contains an entry `{<argname>: (actual_id, case_function)}`. If several argnames are parametrized this way,
14771474
a dedicated entry will be present for each argname.

0 commit comments

Comments
 (0)