Skip to content

Commit b65734b

Browse files
author
Sylvain MARIE
committed
New helper function get_current_case_id to get the current case id for a given pytest request or item. Fixes #189.
1 parent 53e18e3 commit b65734b

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

pytest_cases/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
from .case_funcs import case, copy_case_info, set_case_id, get_case_id, get_case_marks, \
2424
get_case_tags, matches_tag_query, is_case_class, is_case_function
25-
from .case_parametrizer_new import parametrize_with_cases, THIS_MODULE, get_all_cases, get_parametrize_args
25+
from .case_parametrizer_new import parametrize_with_cases, THIS_MODULE, get_all_cases, get_parametrize_args, \
26+
get_current_case_id
2627

2728
try:
2829
# -- Distribution mode --
@@ -67,7 +68,7 @@
6768
'case', 'copy_case_info', 'set_case_id', 'get_case_id', 'get_case_marks',
6869
'get_case_tags', 'matches_tag_query', 'is_case_class', 'is_case_function',
6970
# test functions
70-
'get_all_cases', 'parametrize_with_cases', 'THIS_MODULE', 'get_parametrize_args'
71+
'get_all_cases', 'parametrize_with_cases', 'THIS_MODULE', 'get_parametrize_args', 'get_current_case_id'
7172
]
7273

7374
try: # python 3.5+ type hints

pytest_cases/case_parametrizer_new.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,38 @@ def _of_interest(x): # noqa
720720
return cases
721721

722722

723+
def get_current_case_id(request_or_item,
724+
argnames # type: str
725+
):
726+
"""
727+
A helper function to return the current case id for a given `pytest` item (available in some hooks) or `request`
728+
(available in hooks, and also directly as a fixture).
729+
730+
You need to provide the argname(s) used in the corresponding `@parametrize_with_cases` so that this method finds
731+
the right id.
732+
733+
:param request_or_item:
734+
:param argnames:
735+
:return:
736+
"""
737+
try:
738+
item = request_or_item.node
739+
except AttributeError:
740+
item = request_or_item
741+
742+
try:
743+
# A LazyValue ?
744+
lazy_val = item.callspec.params[argnames]
745+
except KeyError:
746+
# No: A fixture union created by `parametrize_plus_decorate`
747+
main_fixture_style_template = "%s_%s"
748+
fixture_union_name = main_fixture_style_template % (item.function.__name__, argnames)
749+
return item.callspec.params[fixture_union_name].get_alternative_id()
750+
else:
751+
# A lazyvalue - confirmed
752+
return lazy_val.get_id()
753+
754+
723755
# Below is the beginning of a switch from our code scanning tool above to the same one than pytest.
724756
# from .common_pytest import is_fixture, safe_isclass, compat_get_real_func, compat_getfslineno
725757
#
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pytest_cases import parametrize_with_cases, fixture, get_current_case_id
2+
3+
4+
def data_b():
5+
return 1
6+
7+
8+
@parametrize_with_cases("data", cases=data_b, prefix="data_")
9+
def test_lazy_val_case(data, request):
10+
assert get_current_case_id(request, "data") == "b"
11+
12+
13+
@fixture
14+
def a():
15+
return
16+
17+
18+
def data_a(a):
19+
return 1
20+
21+
22+
@parametrize_with_cases("data", cases=data_a, prefix="data_")
23+
def test_fixture_case(data, request):
24+
assert get_current_case_id(request.node, "data") == "a"

0 commit comments

Comments
 (0)