Skip to content

Commit b24ee2b

Browse files
author
Sylvain MARIE
committed
Added a test and improved documentation to explain why @fixture should be used instead of @pytest.fixture. Fixed #125
1 parent 95a1c54 commit b24ee2b

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

docs/api_reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ Transforms a list of cases (obtained from [`get_all_cases`](#get_all_cases)) int
124124

125125
Identical to `@pytest.fixture` decorator, except that
126126

127+
- when used in a fixture union (either explicit `fixture_union` or indirect through `@parametrize`+`fixture_ref` or `@parametrize_with_cases`), it will not be setup/teardown unnecessarily in tests that do not require it.
128+
127129
- it supports multi-parametrization with `@pytest.mark.parametrize` as requested in [pytest#3960](https://github.com/pytest-dev/pytest/issues/3960). As a consequence it does not support the `params` and `ids` arguments anymore.
128130

129131
- it supports a new argument `unpack_into` where you can provide names for fixtures where to unpack this fixture into.

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ test_generators.py::test_foo[simple_generator-who=there] PASSED [100%]
356356

357357
Cases can use fixtures the same way as [test functions do](https://docs.pytest.org/en/stable/fixture.html#fixtures-as-function-arguments): simply add the fixture names as arguments in their signature and make sure the fixture exists either in the same module, or in a [`conftest.py`](https://docs.pytest.org/en/stable/fixture.html?highlight=conftest.py#conftest-py-sharing-fixture-functions) file in one of the parent packages. See [`pytest` documentation on sharing fixtures](https://docs.pytest.org/en/stable/fixture.html?highlight=conftest.py#conftest-py-sharing-fixture-functions).
358358

359+
!!! warning "Use `@fixture` instead of `@pytest.fixture`"
360+
If a fixture is used by *some* of your cases only, then you *should* use the `@fixture` decorator from pytest-cases instead of the standard `@pytest.fixture`. Otherwise you fixture will be setup/teardown for all cases even those not requiring it. See [`@fixture` doc](./api_reference.md#fixture).
359361

360362
```python
361363
from pytest_cases import parametrize_with_cases, fixture, parametrize

pytest_cases/fixture_core2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ def fixture_plus(scope="function", # type: str
277277
278278
Identical to `@pytest.fixture` decorator, except that
279279
280+
- when used in a fixture union (either explicit `fixture_union` or indirect through `@parametrize`+`fixture_ref`
281+
or `@parametrize_with_cases`), it will not be setup/teardown unnecessarily in tests that do not require it.
282+
280283
- it supports multi-parametrization with `@pytest.mark.parametrize` as requested in
281284
https://github.com/pytest-dev/pytest/issues/3960. As a consequence it does not support the `params` and `ids`
282285
arguments anymore.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pytest_cases import parametrize_with_cases, fixture
2+
3+
4+
@fixture
5+
def one():
6+
return 1
7+
8+
9+
time_to_call_two = False
10+
11+
12+
@fixture
13+
def two():
14+
global time_to_call_two
15+
assert time_to_call_two, "Should not be called."
16+
time_to_call_two = False
17+
18+
19+
def case_one(one):
20+
return one
21+
22+
23+
def case_two(two):
24+
return two
25+
26+
27+
def case_three(one):
28+
return one
29+
30+
31+
test_id = 0
32+
33+
34+
@parametrize_with_cases("case", cases=".")
35+
def test(case):
36+
global time_to_call_two, test_id
37+
if test_id == 0:
38+
# next test will need the fixture two
39+
time_to_call_two = True
40+
pass

0 commit comments

Comments
 (0)