Skip to content

Commit 457e9cb

Browse files
author
Sylvain MARIE
committed
Updated doc
1 parent 18fa41d commit 457e9cb

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

docs/index.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
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
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.3937829.svg)](https://doi.org/10.5281/zenodo.3937829)
99

10-
!!! success "New `current_cases` fixture to easily know the current case for each parameter ! See [below](#c-accessing-the-current-case) for details."
10+
!!! success "Slides from the `pytest-cases` presentation at EuroPython 2021 are now [available here](https://ep2021.europython.eu/talks/649sqwq-powerful-tests-and-reproducible-benchmarks-with-pytest-cases/)."
1111

12-
!!! success "Major refactoring of test ids in v3.0.0 ! See [below](#d-test-ids) for details."
13-
14-
!!! success "`@parametrize` now automatically detects fixture symbols ! See [documentation](./pytest_goodies.md#parametrize) for details."
12+
!!! success "New `current_cases` fixture to easily know the current case for each parameter ! See [below](#d-accessing-the-current-case) for details."
1513

1614
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 ?
1715

@@ -413,7 +411,13 @@ test_fixtures.py::test_users[a_is_from_db-id=1]
413411
414412
## Advanced topics
415413
416-
### a- Parametrizing fixtures
414+
### a- Scope of cases
415+
416+
By default a case function is transformed into a lazy *parameter* using [`lazy_value`](api_reference.md#lazy_value). This is not a *fixture*, but simply a new `parametrize` mechanism that allows parameters to be provided by functions (See [`@parametrize`](pytest_goodies.md#parametrize)).
417+
418+
However, as soon as a case function is either parametrized or requires a fixture, then it is automatically transformed into a fixture so that `pytest` can handle it properly. In that situation, the fixture needs to have a scope. By default this scope is `"function"`. You can change it using [the `scope` argument in `@parametrize_with_cases`](api_reference.md#parametrize_with_cases).
419+
420+
### b- Parametrizing fixtures
417421
418422
In some scenarii you might wish to parametrize a fixture with the cases, rather than the test function. For example
419423
@@ -440,15 +444,15 @@ def test_foo(c):
440444
assert isinstance(c, int)
441445
```
442446
443-
### b- Caching cases
447+
### c- Caching cases
444448
445449
After starting to reuse cases in several test functions, you might end-up thinking *"why do I have to spend the data parsing/generation time several times ? It is the same case."*.
446450
447451
`pytest-cases` follows the same philosophy than `pytest`: each test node should be independent. Therefore case functions are called for each test case. This ensures that mutable objects can not leak across tests, for example.
448452
449453
That being said, **if you are certain that your tests do not modify your cases data**, there are several ways to solve this issue:
450454
451-
- the easiest way is to **use fixtures with a broad scope**, as explained [above](#a-parametrizing-fixtures). However in some parametrization scenarii, `pytest` does not guarantee that the fixture will be setup only once for the whole session, even if it is a session-scoped fixture. Also the cases will be parsed everytime you run pytest, which might be cumbersome
455+
- the easiest way is to **use fixtures with a broad scope**, as explained [above](#b-parametrizing-fixtures). However in some parametrization scenarii, `pytest` does not guarantee that the fixture will be setup only once for the whole session, even if it is a session-scoped fixture. Also the cases will be parsed everytime you run pytest, which might be cumbersome
452456
453457
```python
454458
from pytest_cases import parametrize, parametrize_with_cases, fixture
@@ -477,7 +481,7 @@ def test_caching(cached_a, d):
477481
478482
!!! warning "If you add a cache mechanism, make sure that your test functions do not modify the returned objects !"
479483
480-
### c- Accessing the current case
484+
### d- Accessing the current case
481485
482486
In some scenarii you may wish to access the case functions that are currently used to provide the parameter values. This may be
483487
@@ -534,7 +538,7 @@ To get more information on the case function, you can use `get_case_marks(func)`
534538
535539
Note: you can get the same information from a pytest hook, using the `get_current_cases` function. See [API reference](./api_reference.md#get_current_cases) for details.
536540
537-
### d- Test ids
541+
### e- Test ids
538542
539543
Starting from version 3.0.0, test ids induced by `@parametrize_with_cases` are similar to the ids induced by `@pytest.mark.parametrize`, even if a case function is itself parametrized or requires a fixture. In some situations you may wish to get a better control on the test ids.
540544
@@ -580,7 +584,7 @@ test_doc_ids_debug.py::test_foo[#hello_name#-earthling]
580584
============================== 4 passed in 0.07s ==============================
581585
```
582586
583-
### e- Debugging
587+
### f- Debugging
584588
585589
When all of your case functions are simple, `@parametrize_with_cases` generates a `@parametrize` decorator with argvalues being a list of `lazy_value(<case_func>)` for all of them. This in turn falls back to a good old `@pytest.mark.parametrize`, so the behaviour is close to what you are used to see when using `pytest`.
586590
@@ -642,6 +646,8 @@ See also [`@parametrize` documentation](./pytest_goodies.md#parametrize) for det
642646
643647
## See Also
644648
649+
### `pytest`
650+
645651
- [pytest documentation on parametrize](https://docs.pytest.org/en/latest/parametrize.html)
646652
- [pytest documentation on fixtures](https://docs.pytest.org/en/latest/fixture.html#fixture-parametrize)
647653
- [pytest-steps](https://smarie.github.io/python-pytest-steps/)
@@ -650,6 +656,9 @@ See also [`@parametrize` documentation](./pytest_goodies.md#parametrize) for det
650656
651657
### Others
652658
659+
- [makefun](https://smarie.github.io/python-makefun/) is used to dynamically generate functions or modify user-provided function signatures.
660+
- [decopatch](https://smarie.github.io/python-decopatch/) is used to create decorators.
661+
653662
*Do you like this library ? You might also like [my other python libraries](https://github.com/smarie/OVERVIEW#python)*
654663
655664
## Want to contribute ?

0 commit comments

Comments
 (0)