You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
251
255
252
256
As a consequence it does not support the `params` and `ids` arguments anymore.
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.
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)])`.
285
304
@@ -289,7 +308,7 @@ Note that the `(argnames, argvalues, ids)` signature is similar to `@pytest.mark
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.
219
219
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
+
defc(o):
231
+
return o, o[0]
232
+
233
+
a, b = unpack_fixture("a,b", c)
234
+
235
+
deftest_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
+
defc(o):
248
+
return o, o[0]
249
+
250
+
deftest_function(a, b):
251
+
assert a[0] == b
252
+
```
253
+
220
254
### `param_fixture[s]`
221
255
222
256
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.
0 commit comments