def in_unittest():
    current_stack = inspect.stack()
for stack_frame in current_stack:
    for program_line in stack_frame[4]:
        if 'unittest' in program_line:
            return True
return False
How can a piece of python code tell if it's running under unittest