The Problem was :
- ... title : Determine function name (module) from within that function in Python
- In my context of Test Unit but in any other
- without decorator sugar if possible (before industrialisation)
Related to : Determine function name from within that function
But: the impossibility to
- comment <= poor reputation
- add a response <= no reputation
- react to a very old then active, but fragmented answered question...
- feed with this test case !!!
def test_A_01__techreq__labelling__by_introspection():
from_globals = globals()
from_globals__co_name = from_globals[sys._getframe().f_code.co_name]
from_globals__co_name__testcase_module_name = from_globals__co_name.__module__
from_globals__co_name__testcase_name = from_globals__co_name.__name__
from_inspect_code = inspect.currentframe().f_code
from_inspect_code__co_filename = from_inspect_code.co_filename
from_inspect_code__co_filename__but_basename_testcase_module_name = Path(from_inspect_code__co_filename).name
from_inspect_code__co_name__testcase_name = from_inspect_code.co_name
print(
f"\nTestcase Via globals() : "
f"module={from_globals__co_name__testcase_module_name} : "
f"name={from_globals__co_name__testcase_name}")
print(
f"Testcase Via inspects() : "
f"module={from_inspect_code__co_filename__but_basename_testcase_module_name} : "
f"name={from_inspect_code__co_name__testcase_name}")
assert from_globals__co_name__testcase_module_name == "tests.test_05_archi_builder"
assert from_inspect_code__co_filename__but_basename_testcase_module_name == "test_05_archi_builder.py"
assert from_globals__co_name__testcase_name == from_inspect_code__co_name__testcase_name == "test_A_01__techreq__labelling__by_introspection"
test_05_archi_builder.py::test_A_01__techreq__labelling__by_introspection PASSED [100%]
Testcase Via globals() : module=tests.test_05_archi_builder : name=test_A_01__techreq__labelling__by_introspection
Testcase Via inspects() : module=test_05_archi_builder.py : name=test_A_01__techreq__labelling__by_introspection