Skip to content

Commit 61ba6d0

Browse files
author
Sylvain MARIE
committed
Added documentation about @pytest_parametrize_plus and fixture_ref. Added a corresponding test.
1 parent 7a12e6a commit 61ba6d0

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

docs/index.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[![Documentation](https://img.shields.io/badge/doc-latest-blue.svg)](https://smarie.github.io/python-pytest-cases/) [![PyPI](https://img.shields.io/pypi/v/pytest-cases.svg)](https://pypi.python.org/pypi/pytest-cases/) [![Downloads](https://pepy.tech/badge/pytest-cases)](https://pepy.tech/project/pytest-cases) [![Downloads per week](https://pepy.tech/badge/pytest-cases/week)](https://pepy.tech/project/pytest-cases) [![GitHub stars](https://img.shields.io/github/stars/smarie/python-pytest-cases.svg)](https://github.com/smarie/python-pytest-cases/stargazers)
88

9-
!!! success "New `@pytest_fixture_plus` decorator is there, [check it out](#d-case-fixtures) !"
9+
!!! success "New `fixture_union` and `@pytest_parametrize_plus` are there, [check them out](#fixture_union) !"
1010

1111
Did you ever thought that most of your test functions were actually *the same test code*, but with *different data inputs* and expected results/exceptions ?
1212

@@ -275,6 +275,50 @@ This feature has been tested in very complex cases (several union fixtures, fixt
275275
!!! note "fixture unions vs. cases"
276276
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.
277277

278+
### `@pytest_parametrize_plus`
279+
280+
`@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.
281+
282+
For example:
283+
284+
```python
285+
import pytest
286+
from pytest_cases import pytest_parametrize_plus, pytest_fixture_plus, fixture_ref
287+
288+
@pytest.fixture
289+
def world_str():
290+
return 'world'
291+
292+
@pytest_fixture_plus
293+
@pytest_parametrize_plus('who', [fixture_ref(world_str),
294+
'you'])
295+
def greetings(who):
296+
return 'hello ' + who
297+
298+
@pytest_parametrize_plus('main_msg', ['nothing',
299+
fixture_ref(world_str),
300+
fixture_ref(greetings)])
301+
@pytest.mark.parametrize('ending', ['?', '!'])
302+
def test_prints(main_msg, ending):
303+
print(main_msg + ending)
304+
```
305+
306+
yields the following
307+
308+
```bash
309+
> pytest -s -v
310+
nothing?
311+
nothing!
312+
world?
313+
world!
314+
hello world?
315+
hello world!
316+
hello you?
317+
hello you!
318+
```
319+
320+
Note: for this to be performed, the parameters are replaced with a union fixture. Therefore the relative priority order with standard 'mark' parameters will get impacted. You may solve this by replacing your mark parameters with `param_fixture`s (see [above](#param_fixtures).)
321+
278322
## Main features / benefits
279323

280324
* **Separation of concerns**: test code on one hand, test cases data on the other hand. This is particularly relevant for data science projects where a lot of test datasets are used on the same block of test code.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
from pytest_cases import pytest_parametrize_plus, pytest_fixture_plus, fixture_ref
3+
4+
5+
@pytest.fixture
6+
def world_str():
7+
return 'world'
8+
9+
10+
@pytest_fixture_plus
11+
@pytest_parametrize_plus('who', [fixture_ref(world_str), 'you'])
12+
def greetings(who):
13+
return 'hello ' + who
14+
15+
16+
@pytest_parametrize_plus('main_msg', ['nothing', fixture_ref(world_str), fixture_ref(greetings)])
17+
@pytest.mark.parametrize('ending', ['?', '!'])
18+
def test_prints(main_msg, ending):
19+
print(main_msg + ending)
20+
21+
22+
def test_synthesis(module_results_dct):
23+
assert list(module_results_dct) == ['test_prints[test_prints_param__main_msg__0-nothing-?]',
24+
'test_prints[test_prints_param__main_msg__0-nothing-!]',
25+
'test_prints[world_str-?]',
26+
'test_prints[world_str-!]',
27+
'test_prints[greetings-world_str-?]',
28+
'test_prints[greetings-world_str-!]',
29+
'test_prints[greetings-greetings_param__who__1-you-?]',
30+
'test_prints[greetings-greetings_param__who__1-you-!]']

0 commit comments

Comments
 (0)