Skip to content

Commit 8f21460

Browse files
author
Sylvain MARIE
committed
Documentation and docstring updates
1 parent f78c051 commit 8f21460

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

docs/api_reference.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,29 @@ Lists all desired cases for a given user query. This function may be convenient
247247

248248
`pytest_fixture_plus(scope="function", autouse=False, name=None, **kwargs)`
249249

250-
Identical to `@pytest.fixture` decorator, except that it supports multi-parametrization with `@pytest.mark.parametrize` as requested in [pytest#3960](https://github.com/pytest-dev/pytest/issues/3960).
250+
Identical to `@pytest.fixture` decorator, except that
251+
252+
- it supports multi-parametrization with `@pytest.mark.parametrize` as requested in [pytest#3960](https://github.com/pytest-dev/pytest/issues/3960). As a consequence it does not support the `params` and `ids` arguments anymore.
253+
254+
- it supports a new argument `unpack_into` where you can provide names for fixtures where to unpack this fixture into.
251255

252256
As a consequence it does not support the `params` and `ids` arguments anymore.
253257

258+
### `unpack_fixture`
259+
260+
`unpack_fixture(argnames, fixture) -> Tuple[<Fixture>]`
261+
262+
Creates several fixtures with names `argnames` from the source `fixture`. Created fixtures will correspond to elements unpacked from `fixture` in order. For example if `fixture` is a tuple of length 2, `argnames="a,b"` will create two fixtures containing the first and second element respectively.
263+
264+
The created fixtures are automatically registered into the callers' module, but you may wish to assign them to variables for convenience. In that case make sure that you use the same names, e.g. `a, b = unpack_fixture('a,b', 'c')`.
265+
266+
**Parameters**
267+
268+
- **argnames**: same as `@pytest.mark.parametrize` `argnames`.
269+
- **fixture**: a fixture name string or a fixture symbol. If a fixture symbol is provided, the created fixtures will have the same scope. If a name is provided, they will have scope='function'. Note that in practice the performance loss resulting from using `function` rather than a higher scope is negligible since the created fixtures' body is a one-liner.
270+
271+
**Outputs:** the created fixtures.
272+
254273
### `fixture_union`
255274

256275
`fixture_union(name, fixtures, scope="function", idstyle='explicit',
@@ -279,7 +298,7 @@ The style of test ids corresponding to the union alternatives can be changed wit
279298

280299
### `param_fixtures`
281300

282-
`param_fixtures(argnames, argvalues, autouse=False, ids=None, scope="function", **kwargs)`
301+
`param_fixtures(argnames, argvalues, autouse=False, ids=None, scope="function", **kwargs) -> Tuple[<Fixture>]`
283302

284303
Creates one or several "parameters" fixtures - depending on the number or coma-separated names in `argnames`. The created fixtures are automatically registered into the callers' module, but you may wish to assign them to variables for convenience. In that case make sure that you use the same names, e.g. `p, q = param_fixtures('p,q', [(0, 1), (2, 3)])`.
285304

@@ -289,7 +308,7 @@ Note that the `(argnames, argvalues, ids)` signature is similar to `@pytest.mark
289308

290309
### `param_fixture`
291310

292-
`param_fixture(argname, argvalues, autouse=False, ids=None, scope="function", **kwargs)`
311+
`param_fixture(argname, argvalues, autouse=False, ids=None, scope="function", **kwargs) -> <Fixture>`
293312

294313
Identical to `param_fixtures` but for a single parameter name, so that you can assign its output to a single variable.
295314

docs/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,40 @@ This new commandline is a goodie to change the reordering:
217217
!!! note "`@pytest_fixture_plus` deprecation if/when `@pytest.fixture` supports `@pytest.mark.parametrize`"
218218
The ability for pytest fixtures to support the `@pytest.mark.parametrize` annotation is a feature that clearly belongs to `pytest` scope, and has been [requested already](https://github.com/pytest-dev/pytest/issues/3960). It is therefore expected that `@pytest_fixture_plus` will be deprecated in favor of `@pytest_fixture` if/when the `pytest` team decides to add the proposed feature. As always, deprecation will happen slowly across versions (at least two minor, or one major version update) so as for users to have the time to update their code bases.
219219

220+
### `unpack_fixture` / `unpack_into`
221+
222+
In some cases fixtures return a tuple or a list of items. It is not easy to refer to a single of these items in a test or another fixture. With `unpack_fixture` you can easily do it:
223+
224+
```python
225+
import pytest
226+
from pytest_cases import unpack_fixture, pytest_fixture_plus
227+
228+
@pytest_fixture_plus
229+
@pytest.mark.parametrize("o", ['hello', 'world'])
230+
def c(o):
231+
return o, o[0]
232+
233+
a, b = unpack_fixture("a,b", c)
234+
235+
def test_function(a, b):
236+
assert a[0] == b
237+
```
238+
239+
Note that you can also use the `unpack_into=` argument of `@pytest_fixture_plus` to do the same thing:
240+
241+
```python
242+
import pytest
243+
from pytest_cases import pytest_fixture_plus
244+
245+
@pytest_fixture_plus(unpack_into="a,b")
246+
@pytest.mark.parametrize("o", ['hello', 'world'])
247+
def c(o):
248+
return o, o[0]
249+
250+
def test_function(a, b):
251+
assert a[0] == b
252+
```
253+
220254
### `param_fixture[s]`
221255

222256
If you wish to share some parameters across several fixtures and tests, it might be convenient to have a fixture representing this parameter. This is relatively easy for single parameters, but a bit harder for parameter tuples.

pytest_cases/main_fixtures.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,28 @@ def param_fixture(argname, argvalues, autouse=False, ids=None, scope="function",
136136
Identical to `param_fixtures` but for a single parameter name, so that you can assign its output to a single
137137
variable.
138138
139+
```python
140+
import pytest
141+
from pytest_cases import param_fixtures, param_fixture
142+
143+
# create a single parameter fixture
144+
my_parameter = param_fixture("my_parameter", [1, 2, 3, 4])
145+
146+
@pytest.fixture
147+
def fixture_uses_param(my_parameter):
148+
...
149+
150+
def test_uses_param(my_parameter, fixture_uses_param):
151+
...
152+
```
153+
139154
:param argname: see fixture `name`
140155
:param argvalues: see fixture `params`
141156
:param autouse: see fixture `autouse`
142157
:param ids: see fixture `ids`
143158
:param scope: see fixture `scope`
144159
:param kwargs: any other argument for 'fixture'
145-
:return:
160+
:return: the create fixture
146161
"""
147162
if "," in argname:
148163
raise ValueError("`param_fixture` is an alias for `param_fixtures` that can only be used for a single "
@@ -246,13 +261,28 @@ def param_fixtures(argnames, argvalues, autouse=False, ids=None, scope="function
246261
Note that the (argnames, argvalues, ids) signature is similar to `@pytest.mark.parametrize` for consistency,
247262
see https://docs.pytest.org/en/latest/reference.html?highlight=pytest.param#pytest-mark-parametrize
248263
264+
```python
265+
import pytest
266+
from pytest_cases import param_fixtures, param_fixture
267+
268+
# create a 2-tuple parameter fixture
269+
arg1, arg2 = param_fixtures("arg1, arg2", [(1, 2), (3, 4)])
270+
271+
@pytest.fixture
272+
def fixture_uses_param2(arg2):
273+
...
274+
275+
def test_uses_param2(arg1, arg2, fixture_uses_param2):
276+
...
277+
```
278+
249279
:param argnames: same as `@pytest.mark.parametrize` `argnames`.
250280
:param argvalues: same as `@pytest.mark.parametrize` `argvalues`.
251281
:param autouse: see fixture `autouse`
252282
:param ids: same as `@pytest.mark.parametrize` `ids`
253283
:param scope: see fixture `scope`
254284
:param kwargs: any other argument for the created 'fixtures'
255-
:return:
285+
:return: the created fixtures
256286
"""
257287
created_fixtures = []
258288
argnames_lst = get_param_argnames_as_list(argnames)
@@ -281,7 +311,7 @@ def _root_fixture(**kwargs):
281311
# finally create the sub-fixtures
282312
for param_idx, argname in enumerate(argnames_lst):
283313
# create the fixture
284-
# To fix late binding issue with `param_idx` we add an extra layer of scope
314+
# To fix late binding issue with `param_idx` we add an extra layer of scope: a factory function
285315
# See https://stackoverflow.com/questions/3431676/creating-functions-in-a-loop
286316
def _create_fixture(param_idx):
287317
@pytest_fixture_plus(name=argname, scope=scope, autouse=autouse, **kwargs)

0 commit comments

Comments
 (0)