def is_inside_unittest():
"""Test if a function is running from unittest.
This function will help freezing the __setattr__ freezing when running
inside a unittest environment.
"""
frame = inspect.currentframe()
# Calls from unittest discover have the following properties:
# 1) Its stack is longer
# 2) It contains arguments ('argv', 'pkg_name', 'unittest', etc) for
# instance: a key value pair in the format:
# ('argv', ['python3 -m unittest', 'discover', '-vvv', '.'])
key = 'argv' # this may be error prone...
value = 'unittest'
while frame:
frame_argv = frame.f_locals.get(key)
if frame_argv and value in ''.join(frame_argv):
return True
frame = frame.f_back
return False
评论列表
文章目录