Skip to content

Commit

Permalink
Added documentation about @pytest_parametrize_plus and `fixture_ref…
Browse files Browse the repository at this point in the history
…`. Added a corresponding test.
  • Loading branch information
Sylvain MARIE committed Jun 13, 2019
1 parent 7a12e6a commit 61ba6d0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
46 changes: 45 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[![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)

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

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 ?

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

### `@pytest_parametrize_plus`

`@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.

For example:

```python
import pytest
from pytest_cases import pytest_parametrize_plus, pytest_fixture_plus, fixture_ref

@pytest.fixture
def world_str():
return 'world'

@pytest_fixture_plus
@pytest_parametrize_plus('who', [fixture_ref(world_str),
'you'])
def greetings(who):
return 'hello ' + who

@pytest_parametrize_plus('main_msg', ['nothing',
fixture_ref(world_str),
fixture_ref(greetings)])
@pytest.mark.parametrize('ending', ['?', '!'])
def test_prints(main_msg, ending):
print(main_msg + ending)
```

yields the following

```bash
> pytest -s -v
nothing?
nothing!
world?
world!
hello world?
hello world!
hello you?
hello you!
```

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

## Main features / benefits

* **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.
Expand Down
30 changes: 30 additions & 0 deletions pytest_cases/tests/fixtures/test_fixture_in_parametrize_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from pytest_cases import pytest_parametrize_plus, pytest_fixture_plus, fixture_ref


@pytest.fixture
def world_str():
return 'world'


@pytest_fixture_plus
@pytest_parametrize_plus('who', [fixture_ref(world_str), 'you'])
def greetings(who):
return 'hello ' + who


@pytest_parametrize_plus('main_msg', ['nothing', fixture_ref(world_str), fixture_ref(greetings)])
@pytest.mark.parametrize('ending', ['?', '!'])
def test_prints(main_msg, ending):
print(main_msg + ending)


def test_synthesis(module_results_dct):
assert list(module_results_dct) == ['test_prints[test_prints_param__main_msg__0-nothing-?]',
'test_prints[test_prints_param__main_msg__0-nothing-!]',
'test_prints[world_str-?]',
'test_prints[world_str-!]',
'test_prints[greetings-world_str-?]',
'test_prints[greetings-world_str-!]',
'test_prints[greetings-greetings_param__who__1-you-?]',
'test_prints[greetings-greetings_param__who__1-you-!]']

0 comments on commit 61ba6d0

Please sign in to comment.