Skip to content

Commit c3309ed

Browse files
author
Sylvain MARIE
committed
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).
1 parent 4a5c618 commit c3309ed

File tree

5 files changed

+81
-65
lines changed

5 files changed

+81
-65
lines changed

docs/api_reference.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ Lists all desired cases for a given user query. This function may be convenient
243243

244244
## 3 - Pytest goodies
245245

246-
### `@pytest_fixture_plus`
246+
### `@fixture_plus`
247247

248-
`pytest_fixture_plus(scope="function", autouse=False, name=None, unpack_into=None, **kwargs)`
248+
`fixture_plus(scope="function", autouse=False, name=None, unpack_into=None, **kwargs)`
249249

250250
Identical to `@pytest.fixture` decorator, except that
251251

@@ -322,9 +322,9 @@ Note that the `(argnames, argvalues, ids)` signature is similar to `@pytest.mark
322322
Identical to `param_fixtures` but for a single parameter name, so that you can assign its output to a single variable.
323323

324324

325-
### `@pytest_parametrize_plus`
325+
### `@parametrize_plus`
326326

327-
`pytest_parametrize_plus(argnames, argvalues, indirect=False, ids=None, scope=None, **kwargs)`
327+
`parametrize_plus(argnames, argvalues, indirect=False, ids=None, scope=None, **kwargs)`
328328

329329
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.
330330

docs/index.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
!!! warning "Test execution order"
1212
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).
1313

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

1517
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 ?
1618

@@ -111,16 +113,16 @@ You might be concerned that case data is gathered or created *during* test execu
111113

112114
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).
113115

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:
115117

116118
```python
117-
from pytest_cases import pytest_fixture_plus, cases_data
119+
from pytest_cases import fixture_plus, cases_data
118120
from example import foo
119121

120122
# import the module containing the test cases
121123
import test_foo_cases
122124

123-
@pytest_fixture_plus
125+
@fixture_plus
124126
@cases_data(module=test_foo_cases)
125127
def inputs(case_data):
126128
""" Example fixture that is automatically parametrized with @cases_data """
@@ -210,24 +212,24 @@ This new commandline is a goodie to change the reordering:
210212
* `--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.
211213

212214

213-
### `@pytest_fixture_plus`
215+
### `@fixture_plus`
214216

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`).
216218

217219
Finally it now supports unpacking, see [unpacking feature](#unpack_fixture-unpack_into).
218220

219-
!!! note "`@pytest_fixture_plus` deprecation if/when `@pytest.fixture` supports `@pytest.mark.parametrize`"
220-
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.
221+
!!! note "`@fixture_plus` deprecation if/when `@pytest.fixture` supports `@pytest.mark.parametrize`"
222+
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.
221223

222224
### `unpack_fixture` / `unpack_into`
223225

224226
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:
225227

226228
```python
227229
import pytest
228-
from pytest_cases import unpack_fixture, pytest_fixture_plus
230+
from pytest_cases import unpack_fixture, fixture_plus
229231

230-
@pytest_fixture_plus
232+
@fixture_plus
231233
@pytest.mark.parametrize("o", ['hello', 'world'])
232234
def c(o):
233235
return o, o[0]
@@ -238,13 +240,13 @@ def test_function(a, b):
238240
assert a[0] == b
239241
```
240242

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:
242244

243245
```python
244246
import pytest
245-
from pytest_cases import pytest_fixture_plus
247+
from pytest_cases import fixture_plus
246248

247-
@pytest_fixture_plus(unpack_into="a,b")
249+
@fixture_plus(unpack_into="a,b")
248250
@pytest.mark.parametrize("o", ['hello', 'world'])
249251
def c(o):
250252
return o, o[0]
@@ -257,14 +259,14 @@ And it is also available in `fixture_union`:
257259

258260
```python
259261
import pytest
260-
from pytest_cases import pytest_fixture_plus, fixture_union
262+
from pytest_cases import fixture_plus, fixture_union
261263

262-
@pytest_fixture_plus
264+
@fixture_plus
263265
@pytest.mark.parametrize("o", ['hello', 'world'])
264266
def c(o):
265267
return o, o[0]
266268

267-
@pytest_fixture_plus
269+
@fixture_plus
268270
@pytest.mark.parametrize("o", ['yeepee', 'yay'])
269271
def d(o):
270272
return o, o[0]
@@ -316,13 +318,13 @@ The topic has been largely discussed in [pytest-dev#349](https://github.com/pyte
316318
`fixture_union` is an implementation of this proposal.
317319

318320
```python
319-
from pytest_cases import pytest_fixture_plus, fixture_union
321+
from pytest_cases import fixture_plus, fixture_union
320322

321-
@pytest_fixture_plus
323+
@fixture_plus
322324
def first():
323325
return 'hello'
324326

325-
@pytest_fixture_plus(params=['a', 'b'])
327+
@fixture_plus(params=['a', 'b'])
326328
def second(request):
327329
return request.param
328330

@@ -345,34 +347,34 @@ As you can see the ids of union fixtures are slightly different from standard id
345347

346348
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 !
347349

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

350352
!!! note "fixture unions vs. cases"
351353
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.
352354

353355
Finally fixture unions now supports unpacking, see [unpacking feature](#unpack_fixture-unpack_into).
354356

355-
### `@pytest_parametrize_plus`
357+
### `@parametrize_plus`
356358

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

359361
For example:
360362

361363
```python
362364
import pytest
363-
from pytest_cases import pytest_parametrize_plus, pytest_fixture_plus, fixture_ref
365+
from pytest_cases import parametrize_plus, fixture_plus, fixture_ref
364366

365367
@pytest.fixture
366368
def world_str():
367369
return 'world'
368370

369-
@pytest_fixture_plus
370-
@pytest_parametrize_plus('who', [fixture_ref(world_str),
371+
@fixture_plus
372+
@parametrize_plus('who', [fixture_ref(world_str),
371373
'you'])
372374
def greetings(who):
373375
return 'hello ' + who
374376

375-
@pytest_parametrize_plus('main_msg', ['nothing',
377+
@parametrize_plus('main_msg', ['nothing',
376378
fixture_ref(world_str),
377379
fixture_ref(greetings)])
378380
@pytest.mark.parametrize('ending', ['?', '!'])

pytest_cases/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pytest_cases.case_funcs import case_name, test_target, case_tags, cases_generator
22

3-
from pytest_cases.main_fixtures import cases_fixture, pytest_fixture_plus, param_fixtures, param_fixture, \
4-
fixture_union, NOT_USED, pytest_parametrize_plus, fixture_ref, unpack_fixture
3+
from pytest_cases.main_fixtures import cases_fixture, fixture_plus, param_fixtures, param_fixture, \
4+
fixture_union, NOT_USED, parametrize_plus, fixture_ref, unpack_fixture, pytest_fixture_plus, pytest_parametrize_plus
55

66
from pytest_cases.main_params import cases_data, CaseDataGetter, unfold_expected_err, get_all_cases, THIS_MODULE, \
77
get_pytest_parametrize_args
@@ -25,8 +25,8 @@
2525
# --cases_funcs
2626
'case_name', 'test_target', 'case_tags', 'cases_generator',
2727
# --main_fixtures
28-
'cases_fixture', 'pytest_fixture_plus', 'param_fixtures', 'param_fixture',
29-
'fixture_union', 'NOT_USED', 'pytest_parametrize_plus', 'fixture_ref', 'unpack_fixture',
28+
'cases_fixture', 'pytest_fixture_plus', 'fixture_plus', 'param_fixtures', 'param_fixture',
29+
'fixture_union', 'NOT_USED', 'pytest_parametrize_plus', 'parametrize_plus', 'fixture_ref', 'unpack_fixture',
3030
# --main params
3131
'cases_data', 'CaseDataGetter', 'THIS_MODULE', 'unfold_expected_err', 'get_all_cases',
3232
'get_pytest_parametrize_args',

pytest_cases/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ def get_param_argnames_as_list(argnames):
8282
return list(argnames)
8383

8484

85-
# ------------ container for the mark information that we grab from the fixtures (`@pytest_fixture_plus`)
85+
# ------------ container for the mark information that we grab from the fixtures (`@fixture_plus`)
8686
class _ParametrizationMark:
8787
"""
88-
Represents the information required by `@pytest_fixture_plus` to work.
88+
Represents the information required by `@fixture_plus` to work.
8989
"""
9090
__slots__ = "param_names", "param_values", "param_ids"
9191

0 commit comments

Comments
 (0)