Skip to content

Commit 14182ed

Browse files
author
Sylvain MARIE
committed
Fixed minor issue where empty entries could be present in currentcases. Fixes #213
1 parent 70b034d commit 14182ed

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

pytest_cases/case_parametrizer_new.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,13 @@ def get_current_cases(request_or_item):
879879
argval = LazyTupleItem(argvals, item)
880880
_possibly_add_cases_to_results(request, fix_results, mp_fix_to_args, argname, argval)
881881
else:
882-
print()
882+
# not lazy = normal parameter = there is no way this is a case
883+
pass
884+
885+
# cleanup: delete the empty subdict if the params were not cases.
886+
if len(fix_results) == 0:
887+
del results[argname_or_fixturename]
888+
883889

884890
# (2) Parameters on a test function, or parameters on a fixture with a fixture_ref inside (other fixture gen)
885891
else:
@@ -944,9 +950,18 @@ def _possibly_add_cases_to_results(request, results, mp_fix_to_args, argname_or_
944950
return
945951

946952
# If the parametrization target is not the test but a fixture, store the cases in a dub-dict
953+
orig_results = None
947954
if parametrized is not None and parametrized.__name__ != request.node.function.__name__:
955+
# store the wrapping dict because we'll delete this entry in the end if it is empty
956+
orig_results = results
948957
results = _get_or_create_subdict(results, parametrized.__name__)
949958

959+
# this function will need to be called before returning
960+
def _cleanup_before_return():
961+
# remove the subdict if it was created for nothing
962+
if orig_results is not None and len(results) == 0:
963+
del orig_results[parametrized.__name__]
964+
950965
# If we did not yet find the case function, this is because this was a simple parametrize without fixture ref.
951966
if case_func is None:
952967
if isinstance(current_param_value, LazyTupleItem):
@@ -964,10 +979,12 @@ def _possibly_add_cases_to_results(request, results, mp_fix_to_args, argname_or_
964979

965980
elif current_param_value in (NOT_USED, USED):
966981
# ignore silently
982+
_cleanup_before_return()
967983
return
968984
else:
969985
# raise TypeError("Internal error - type not expected : %r" % type(current_param_value))
970986
# some other parameter - return silently
987+
_cleanup_before_return()
971988
return
972989

973990
# Finally do it
@@ -981,6 +998,8 @@ def _possibly_add_cases_to_results(request, results, mp_fix_to_args, argname_or_
981998
raise ValueError("Internal error - please report")
982999
results[argname] = (actual_id, case_func)
9831000

1001+
_cleanup_before_return()
1002+
9841003

9851004
def _get_or_create_subdict(dct, key):
9861005
try:

pytest_cases/tests/cases/doc/test_get_current_cases_negative.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# + All contributors to <https://github.com/smarie/python-pytest-cases>
33
#
44
# License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
5-
from pytest_cases import fixture, get_current_cases, parametrize, lazy_value, fixture_ref
5+
from pytest_cases import fixture, get_current_cases, parametrize, lazy_value, fixture_ref, parametrize_with_cases
66

77

88
def foo():
@@ -20,3 +20,32 @@ def foo_fix():
2020
def test_foo(a, a1, a2, b1, b2, current_cases, request):
2121
assert current_cases == {}
2222
assert get_current_cases(request) == {}
23+
24+
25+
# ----------- fix for issue 213
26+
27+
@parametrize(name=("bar", ))
28+
def case_foo2(name):
29+
return name
30+
31+
32+
@parametrize_with_cases("a", cases=case_foo2)
33+
def test_foo2(a, current_cases):
34+
assert current_cases == {'a': ('foo2', case_foo2)}
35+
36+
37+
# ----------- fix for issue 213 bis
38+
39+
@fixture
40+
def o():
41+
return "name"
42+
43+
44+
@fixture
45+
@parametrize("a", (fixture_ref(o), 'r'))
46+
def a_fix(a):
47+
return a
48+
49+
50+
def test_foo3(a_fix, current_cases):
51+
assert current_cases == {}

0 commit comments

Comments
 (0)