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
Copy file name to clipboardExpand all lines: docs/index.md
+19-10Lines changed: 19 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,9 @@
7
7
[](https://smarie.github.io/python-pytest-cases/)[](https://pypi.python.org/pypi/pytest-cases/)[](https://pepy.tech/project/pytest-cases)[](https://pepy.tech/project/pytest-cases)[](https://github.com/smarie/python-pytest-cases/stargazers)
!!! 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/)."
11
11
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."
15
13
16
14
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 ?
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
417
421
418
422
In some scenarii you might wish to parametrize a fixture with the cases, rather than the test function. For example
419
423
@@ -440,15 +444,15 @@ def test_foo(c):
440
444
assertisinstance(c, int)
441
445
```
442
446
443
-
### b- Caching cases
447
+
### c- Caching cases
444
448
445
449
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."*.
446
450
447
451
`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.
448
452
449
453
That being said, **if you are certain that your tests do not modify your cases data**, there are several ways to solve this issue:
450
454
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
452
456
453
457
```python
454
458
from pytest_cases import parametrize, parametrize_with_cases, fixture
!!! warning "If you add a cache mechanism, make sure that your test functions do not modify the returned objects !"
479
483
480
-
### c- Accessing the current case
484
+
### d- Accessing the current case
481
485
482
486
In some scenarii you may wish to access the case functions that are currently used to provide the parameter values. This may be
483
487
@@ -534,7 +538,7 @@ To get more information on the case function, you can use `get_case_marks(func)`
534
538
535
539
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.
536
540
537
-
### d- Test ids
541
+
### e- Test ids
538
542
539
543
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.
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>)`forall 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`.
586
590
@@ -642,6 +646,8 @@ See also [`@parametrize` documentation](./pytest_goodies.md#parametrize) for det
642
646
643
647
## See Also
644
648
649
+
### `pytest`
650
+
645
651
- [pytest documentation on parametrize](https://docs.pytest.org/en/latest/parametrize.html)
646
652
- [pytest documentation on fixtures](https://docs.pytest.org/en/latest/fixture.html#fixture-parametrize)
0 commit comments