Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created April 11, 2022 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kezabelle/eaf852254bd824c9a94c65664676dbbf to your computer and use it in GitHub Desktop.
Save kezabelle/eaf852254bd824c9a94c65664676dbbf to your computer and use it in GitHub Desktop.
What test methods (python unittest style) call this? A dirty hack of a snippet for me to re-use as I explore a new project.
for frame in inspect.getouterframes(inspect.currentframe()):
if frame.function.startswith("test_"):
repr = "<unknown>"
caller = ""
if "self" in frame.frame.f_locals:
caller = frame.frame.f_locals["self"]
elif "cls" in frame.frame.f_locals:
caller = frame.frame.f_locals["cls"]
if caller:
if not hasattr(caller, "__name__"):
repr = f"{caller.__module__}.{caller.__class__.__name__}"
else:
repr = f"{caller.__module__}.{caller.__qualname__}"
print(frame.filename, frame.lineno, f"{repr}.{frame.function}")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment