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
According to discussion in pytest-dev/pytest#6475 and #71: pytest_fixture_plus and pytest_parametrize_plus were renamed to fixture_plus and parametrize_plus in order for pytest (pluggy) not to think they were hooks. Old aliases will stay around for a few versions, with a deprecation warning. See [#71](#71).
Equivalent to `@pytest.mark.parametrize` but also supports the fact that in argvalues one can include references to fixtures with `fixture_ref(<fixture>)` where <fixture> can be the fixture name or fixture function.
Copy file name to clipboardExpand all lines: docs/index.md
+27-25Lines changed: 27 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,8 @@
11
11
!!! warning "Test execution order"
12
12
Installing pytest-cases now has effects on the order of `pytest` tests execution, even if you do not use its features. One positive side effect is that it fixed [pytest#5054](https://github.com/pytest-dev/pytest/issues/5054). But if you see less desirable ordering please [report it](https://github.com/smarie/python-pytest-cases/issues).
13
13
14
+
!!! warning "New aliases"
15
+
`pytest_fixture_plus` and `pytest_parametrize_plus` were renamed to `fixture_plus` and `parametrize_plus` in order for pytest (pluggy) not to think they were hooks. Old aliases will stay around for a few versions, with a deprecation warning. See [#71](https://github.com/smarie/python-pytest-cases/issues/71).
14
16
15
17
Did you ever think that most of your test functions were actually *the same test code*, but with *different data inputs* and expected results/exceptions ?
16
18
@@ -111,16 +113,16 @@ You might be concerned that case data is gathered or created *during* test execu
111
113
112
114
Indeed creating or collecting case data is not part of the test *per se*. Besides, if you benchmark your tests durations (for example with [pytest-harvest](https://smarie.github.io/python-pytest-harvest/)), you may want the test duration to be computed without acccounting for the data retrieval time - especially if you decide to add some caching mechanism as explained [here](https://smarie.github.io/python-pytest-cases/usage/advanced/#caching).
113
115
114
-
It might therefore be more interesting for you to parametrize **case fixtures** instead of parametrizing your test function. Thanks to our new [`@pytest_fixture_plus`](#pytest_fixture_plus) decorator, this works exactly the same way than for test functions:
116
+
It might therefore be more interesting for you to parametrize **case fixtures** instead of parametrizing your test function. Thanks to our new [`@fixture_plus`](#pytest_fixture_plus) decorator, this works exactly the same way than for test functions:
115
117
116
118
```python
117
-
from pytest_cases importpytest_fixture_plus, cases_data
119
+
from pytest_cases importfixture_plus, cases_data
118
120
from example import foo
119
121
120
122
# import the module containing the test cases
121
123
import test_foo_cases
122
124
123
-
@pytest_fixture_plus
125
+
@fixture_plus
124
126
@cases_data(module=test_foo_cases)
125
127
definputs(case_data):
126
128
""" Example fixture that is automatically parametrized with @cases_data """
@@ -210,24 +212,24 @@ This new commandline is a goodie to change the reordering:
210
212
*`--with-reorder skip` allows you to restore the original order that was active before `pytest_collection_modifyitems` was initially called, thus not taking into account any reordering done by pytest or by any of its plugins.
211
213
212
214
213
-
### `@pytest_fixture_plus`
215
+
### `@fixture_plus`
214
216
215
-
`@pytest_fixture_plus` is similar to `pytest.fixture` but without its `param` and `ids` arguments. Instead, it is able to pick the parametrization from `@pytest.mark.parametrize` marks applied on fixtures. This makes it very intuitive for users to parametrize both their tests and fixtures. As a bonus, its `name` argument works even in old versions of pytest (which is not the case for `fixture`).
217
+
`@fixture_plus` is similar to `pytest.fixture` but without its `param` and `ids` arguments. Instead, it is able to pick the parametrization from `@pytest.mark.parametrize` marks applied on fixtures. This makes it very intuitive for users to parametrize both their tests and fixtures. As a bonus, its `name` argument works even in old versions of pytest (which is not the case for `fixture`).
216
218
217
219
Finally it now supports unpacking, see [unpacking feature](#unpack_fixture-unpack_into).
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.
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 `@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.
221
223
222
224
### `unpack_fixture` / `unpack_into`
223
225
224
226
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:
225
227
226
228
```python
227
229
import pytest
228
-
from pytest_cases import unpack_fixture, pytest_fixture_plus
230
+
from pytest_cases import unpack_fixture, fixture_plus
229
231
230
-
@pytest_fixture_plus
232
+
@fixture_plus
231
233
@pytest.mark.parametrize("o", ['hello', 'world'])
232
234
defc(o):
233
235
return o, o[0]
@@ -238,13 +240,13 @@ def test_function(a, b):
238
240
assert a[0] == b
239
241
```
240
242
241
-
Note that you can also use the `unpack_into=` argument of `@pytest_fixture_plus` to do the same thing:
243
+
Note that you can also use the `unpack_into=` argument of `@fixture_plus` to do the same thing:
242
244
243
245
```python
244
246
import pytest
245
-
from pytest_cases importpytest_fixture_plus
247
+
from pytest_cases importfixture_plus
246
248
247
-
@pytest_fixture_plus(unpack_into="a,b")
249
+
@fixture_plus(unpack_into="a,b")
248
250
@pytest.mark.parametrize("o", ['hello', 'world'])
249
251
defc(o):
250
252
return o, o[0]
@@ -257,14 +259,14 @@ And it is also available in `fixture_union`:
257
259
258
260
```python
259
261
import pytest
260
-
from pytest_cases importpytest_fixture_plus, fixture_union
262
+
from pytest_cases importfixture_plus, fixture_union
261
263
262
-
@pytest_fixture_plus
264
+
@fixture_plus
263
265
@pytest.mark.parametrize("o", ['hello', 'world'])
264
266
defc(o):
265
267
return o, o[0]
266
268
267
-
@pytest_fixture_plus
269
+
@fixture_plus
268
270
@pytest.mark.parametrize("o", ['yeepee', 'yay'])
269
271
defd(o):
270
272
return o, o[0]
@@ -316,13 +318,13 @@ The topic has been largely discussed in [pytest-dev#349](https://github.com/pyte
316
318
`fixture_union` is an implementation of this proposal.
317
319
318
320
```python
319
-
from pytest_cases importpytest_fixture_plus, fixture_union
321
+
from pytest_cases importfixture_plus, fixture_union
320
322
321
-
@pytest_fixture_plus
323
+
@fixture_plus
322
324
deffirst():
323
325
return'hello'
324
326
325
-
@pytest_fixture_plus(params=['a', 'b'])
327
+
@fixture_plus(params=['a', 'b'])
326
328
defsecond(request):
327
329
return request.param
328
330
@@ -345,34 +347,34 @@ As you can see the ids of union fixtures are slightly different from standard id
345
347
346
348
This feature has been tested in very complex cases (several union fixtures, fixtures that are not selected by a given union but that is requested by the test function, etc.). But if you find some strange behaviour don't hesitate to report it in the [issues](https://github.com/smarie/python-pytest-cases/issues) page !
347
349
348
-
**IMPORTANT** if you do not use `@pytest_fixture_plus` but only `@pytest.fixture`, then you will see that your fixtures are called even when they are not used, with a parameter `NOT_USED`. This symbol is automatically ignored if you use `@pytest_fixture_plus`, otherwise you have to handle it.
350
+
**IMPORTANT** if you do not use `@fixture_plus` but only `@pytest.fixture`, then you will see that your fixtures are called even when they are not used, with a parameter `NOT_USED`. This symbol is automatically ignored if you use `@fixture_plus`, otherwise you have to handle it.
349
351
350
352
!!! note "fixture unions vs. cases"
351
353
If you're familiar with `pytest-cases` already, you might note `@cases_data` is not so different than a fixture union: we do a union of all case functions. If one day union fixtures are directly supported by `pytest`, we will probably refactor this lib to align all the concepts.
352
354
353
355
Finally fixture unions now supports unpacking, see [unpacking feature](#unpack_fixture-unpack_into).
354
356
355
-
### `@pytest_parametrize_plus`
357
+
### `@parametrize_plus`
356
358
357
-
`@pytest_parametrize_plus` is a replacement for `@pytest.mark.parametrize` that allows you to include references to fixtures in the parameter values. Simply use `fixture_ref(<fixture>)` in the parameter values, where `<fixture>` can be the fixture name or fixture function.
359
+
`@parametrize_plus` is a replacement for `@pytest.mark.parametrize` that allows you to include references to fixtures in the parameter values. Simply use `fixture_ref(<fixture>)` in the parameter values, where `<fixture>` can be the fixture name or fixture function.
358
360
359
361
For example:
360
362
361
363
```python
362
364
import pytest
363
-
from pytest_cases importpytest_parametrize_plus, pytest_fixture_plus, fixture_ref
365
+
from pytest_cases importparametrize_plus, fixture_plus, fixture_ref
0 commit comments