Skip to content

Commit def94ce

Browse files
author
Sylvain MARIE
committed
Fixed and updated import_default_cases_module
1 parent cadffa2 commit def94ce

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/pytest_cases/case_parametrizer_new.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def get_all_cases(parametrization_target=None, # type: Callable
300300
# module
301301
if c is AUTO:
302302
# First try `test_<name>_cases.py` Then `case_<name>.py`
303-
c = import_default_cases_module(parametrization_target)
303+
c = import_default_cases_module(caller_module_name)
304304

305305
elif c is THIS_MODULE or c == '.':
306306
c = caller_module_name
@@ -647,34 +647,26 @@ def _get_fixture_cases(module_or_class # type: Union[ModuleType, Type]
647647
return cache, imported_fixtures_list
648648

649649

650-
def import_default_cases_module(context):
650+
def import_default_cases_module(test_module_name):
651651
"""
652-
Implements the `module=AUTO` behaviour of `@parameterize_cases`: based on the context
653-
passed in. This can either a <module> object or a decorated test function in which
654-
case it finds its containing module name "test_<module>.py" and then tries to import
655-
the python module "test_<module>_cases.py".
652+
Implements the `module=AUTO` behaviour of `@parameterize_cases`.
656653
657-
If "test_<module>_cases.py" module is not found it looks for the alternate
658-
file `cases_<module>.py`.
654+
`test_module_name` will have the format "test_<module>.py", the associated python module "test_<module>_cases.py"
655+
will be loaded to load the cases.
659656
660-
:param f: the decorated test function or a module
657+
If "test_<module>_cases.py" module is not found it looks for the alternate file `cases_<module>.py`.
658+
659+
:param test_module_name: the test module
661660
:return:
662661
"""
663-
if ismodule(context):
664-
module_name = context.__name__
665-
elif hasattr(context, "__module__"):
666-
module_name = context.__module__
667-
else:
668-
raise ValueError("Can't get module from context %s" % context)
669-
670662
# First try `test_<name>_cases.py`
671-
cases_module_name1 = "%s_cases" % module_name
663+
cases_module_name1 = "%s_cases" % test_module_name
672664

673665
try:
674666
cases_module = import_module(cases_module_name1)
675667
except ModuleNotFoundError:
676668
# Then try `case_<name>.py`
677-
parts = module_name.split('.')
669+
parts = test_module_name.split('.')
678670
assert parts[-1][0:5] == 'test_'
679671
cases_module_name2 = "%s.cases_%s" % ('.'.join(parts[:-1]), parts[-1][5:])
680672
try:
@@ -684,7 +676,7 @@ def import_default_cases_module(context):
684676
raise ValueError("Error importing test cases module to parametrize %r: unable to import AUTO "
685677
"cases module %r nor %r. Maybe you wish to import cases from somewhere else ? In that case"
686678
"please specify `cases=...`."
687-
% (f, cases_module_name1, cases_module_name2))
679+
% (test_module_name, cases_module_name1, cases_module_name2))
688680

689681
return cases_module
690682

0 commit comments

Comments
 (0)