Skip to content

Commit 5711646

Browse files
author
Sylvain MARIE
committed
Updated documentation about current_cases and associated test
1 parent 43544fd commit 5711646

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

docs/index.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,11 @@ In some scenarii you may wish to access the case functions that are currently us
488488
With `pytest-cases` starting in version `3.5`, this is now possible thanks to the `current_cases` fixture. Simply use this fixture to get a dictionary containing the actual parameter id and case function for all parameters parametrized with cases in the current test node. Parametrized fixtures, if any, will appear in a sub-dictionary indexed by the fixture name.
489489
490490
```python
491-
from pytest_cases import parametrize_with_cases, fixture
491+
from pytest_cases import parametrize, parametrize_with_cases, fixture
492492
493+
@parametrize(nb=(1,))
493494
def case_a():
494-
return 1
495+
return nb
495496
496497
@fixture
497498
@parametrize_with_cases("foo", cases=case_a)
@@ -501,24 +502,28 @@ def my_fixture(foo):
501502
@parametrize_with_cases("data", cases=case_a)
502503
def test_get_current_case(data, my_fixture, current_cases):
503504
504-
# this is how to access the case function for a test parameter
505-
actual_case_id, case_fun = current_cases["data"]
505+
# access the case details for a test parameter
506+
data_id, data_fun, data_params = current_cases["data"]
506507
507-
# this is how to access the case function for a fixture parameter
508-
fix_actual_case_id, fix_case_fun = current_cases["my_fixture"]["foo"]
508+
# access the case details for a fixture parameter
509+
my_fixture_id, my_fixture_fun, my_fixture_params = current_cases["my_fixture"]["foo"]
509510
510-
# let's print everything
511+
# let's print all case information for this test node
511512
print(current_cases)
512513
```
513514
514515
yields
515516
516517
```
517-
{'data': ('a', <function case_a at 0x00000205BED1CF28>),
518-
'my_fixture': {'foo': ('a', <function case_a at 0x00000205BED1CF28>)}}
518+
{'data': Case(id='a', func=<function case_a at 0x000001C0CAE9E700>, params={'nb': 1}),
519+
'my_fixture': {
520+
'foo': Case(id='a', func=<function case_a at 0x000001C0CAE9E700>, params={'nb': 1})
521+
}}
519522
```
520523
521-
To get more information on the case function, you can use `get_case_id(f)`, `get_case_marks(f)`, `get_case_tags(f)`. You can also use `matches_tag_query` to check if a case function matches some expectations either concerning its id or its tags. See [API reference](./api_reference.md#matches_tag_query).
524+
As you can see above, details are provided as `namedtuple`s. When a case itself is parametrized, its current parameter value(s) appear too (in the above example, `case_a` is parametrized with `nb`).
525+
526+
To get more information on the case function, you can use `get_case_marks(func)`, `get_case_tags(func)`. You can also use `matches_tag_query(...)` to check if a case function matches some expectations either concerning its id or its tags. See [API reference](./api_reference.md#matches_tag_query).
522527
523528
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.
524529

pytest_cases/tests/cases/doc/test_doc_get_current_cases.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from pytest_cases import parametrize_with_cases, fixture
1+
from pytest_cases import parametrize_with_cases, fixture, parametrize
22

33

4-
def case_a():
5-
return 1
4+
@parametrize(nb=(1,))
5+
def case_a(nb):
6+
return nb
67

78

89
@fixture
@@ -24,9 +25,9 @@ def test_get_current_case(data, my_fixture, current_cases):
2425
print(current_cases)
2526

2627
assert current_cases == {
27-
"data": ("a", case_a, {}),
28+
"data": ("a", case_a, {'nb': 1}),
2829
"my_fixture": {
29-
"foo": ("a", case_a, {})
30+
"foo": ("a", case_a, {'nb': 1})
3031
}
3132
}
3233

0 commit comments

Comments
 (0)