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
Copy file name to clipboardExpand all lines: docs/api_reference.md
+22-2Lines changed: 22 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -380,7 +380,7 @@ As a consequence it does not support the `params` and `ids` arguments anymore.
380
380
-**scope**: the scope for which this fixture is shared, one of "function" (default), "class", "module" or "session".
381
381
-**autouse**: if True, the fixture func is activated for all tests that can see it. If False (the default) then an explicitreference is needed to activate the fixture.
382
382
-**name**: the name of the fixture. This defaults to the name of the decorated function. Note: If a fixture is used in the same module in which it is defined, the function name of the fixture will be shadowed by the function arg that requests the fixture; one wayto resolve this is to name the decorated function ``fixture_<fixturename>`` and then use ``@pytest.fixture(name='<fixturename>')``.
383
-
-**unpack_into**: an optional iterable of names, or string containing coma-separated names, for additional fixtures to create to represent parts of this fixture. See `unpack_fixture` for details.
383
+
-**unpack_into**: an optional iterable of names, or string containing coma-separated names, for additional fixtures to create to represent parts of this fixture. See [`unpack_fixture`](#unpack_fixture) for details.
384
384
-**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.
385
385
-**kwargs**: other keyword arguments for `@pytest.fixture`
386
386
@@ -389,8 +389,9 @@ As a consequence it does not support the `params` and `ids` arguments anymore.
389
389
```python
390
390
defunpack_fixture(argnames: str,
391
391
fixture: Union[str, Callable],
392
+
in_cls: bool=False,
392
393
hook: Callable =None
393
-
) -> Tuple[<Fixture>]
394
+
) -> Tuple[<Fixture>, ...]
394
395
```
395
396
396
397
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.
@@ -412,10 +413,29 @@ def test_function(a, b):
412
413
assert a[0] == b
413
414
```
414
415
416
+
You can also use this function inside a class with `in_cls=True`. In that case you MUST assign the output of the function to variables, as the created fixtures won't be registered with the encompassing module.
417
+
418
+
```python
419
+
import pytest
420
+
from pytest_cases import unpack_fixture, fixture
421
+
422
+
@fixture
423
+
@pytest.mark.parametrize("o", ['hello', 'world'])
424
+
defc(o):
425
+
return o, o[0]
426
+
427
+
classTestClass:
428
+
a, b = unpack_fixture("a,b", c, in_cls=True)
429
+
430
+
deftest_function(self, a, b):
431
+
assert a[0] == b
432
+
```
433
+
415
434
**Parameters**
416
435
417
436
-**argnames**: same as `@pytest.mark.parametrize``argnames`.
418
437
-**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.
438
+
-**in_cls**: a boolean (default `False`). You may wish to turn this to `True` to use this function inside a class. If you do so, you **MUST** assign the output to variables in the class.
419
439
-**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.
0 commit comments