@nottest
def run_tests_if_main(measure_mem=False):
 local_vars = inspect.currentframe().f_back.f_locals
 if (not (local_vars.get('__name__', '') == '__main__')):
  return
 try:
  import faulthandler
  faulthandler.enable()
 except Exception:
  pass
 with warnings.catch_warnings(record=True):
  mem = (int(round(max(memory_usage((-1))))) if measure_mem else (-1))
 if (mem >= 0):
  print(('Memory consumption after import: %s' % mem))
 t0 = time.time()
 (peak_mem, peak_name) = (mem, 'import')
 (max_elapsed, elapsed_name) = (0, 'N/A')
 count = 0
 for name in sorted(list(local_vars.keys()), key=(lambda x: x.lower())):
  val = local_vars[name]
  if name.startswith('_'):
   continue
  elif (callable(val) and name.startswith('test')):
   count += 1
   doc = (val.__doc__.strip() if val.__doc__ else name)
   sys.stdout.write(('%s ... ' % doc))
   sys.stdout.flush()
   try:
    t1 = time.time()
    if measure_mem:
     with warnings.catch_warnings(record=True):
      mem = int(round(max(memory_usage((val, (), {})))))
    else:
     val()
     mem = (-1)
    if (mem >= peak_mem):
     (peak_mem, peak_name) = (mem, name)
    mem = ((', mem: %s MB' % mem) if (mem >= 0) else '')
    elapsed = int(round((time.time() - t1)))
    if (elapsed >= max_elapsed):
     (max_elapsed, elapsed_name) = (elapsed, name)
    sys.stdout.write(('time: %s sec%s\n' % (elapsed, mem)))
    sys.stdout.flush()
   except Exception as err:
    if ('skiptest' in err.__class__.__name__.lower()):
     sys.stdout.write(('SKIP (%s)\n' % str(err)))
     sys.stdout.flush()
    else:
     raise
 elapsed = int(round((time.time() - t0)))
 sys.stdout.write(('Total: %s tests\n\xe2\x80\xa2 %s sec (%s sec for %s)\n\xe2\x80\xa2 Peak memory %s MB (%s)\n' % (count, elapsed, max_elapsed, elapsed_name, peak_mem, peak_name)))

@ nottest def run_tests_if_main ( measure_mem = False ) : local_vars = inspect . currentframe ( ) . f_back . f_locals if ( not ( local_vars . get ( '__name__' , '' ) == '__main__' ) ) : return try : import faulthandler faulthandler . enable ( ) except Exception : pass with warnings . catch_warnings ( record = True ) : mem = ( int ( round ( max ( memory_usage ( ( - 1 ) ) ) ) ) if measure_mem else ( - 1 ) ) if ( mem >= 0 ) : print ( ( 'Memory consumption after import: %s' % mem ) ) t0 = time . time ( ) ( peak_mem , peak_name ) = ( mem , 'import' ) ( max_elapsed , elapsed_name ) = ( 0 , 'N/A' ) count = 0 for name in sorted ( list ( local_vars . keys ( ) ) , key = ( lambda x : x . lower ( ) ) ) : val = local_vars [ name ] if name . startswith ( '_' ) : continue elif ( callable ( val ) and name . startswith ( 'test' ) ) : count += 1 doc = ( val . __doc__ . strip ( ) if val . __doc__ else name ) sys . stdout . write ( ( '%s ... ' % doc ) ) sys . stdout . flush ( ) try : t1 = time . time ( ) if measure_mem : with warnings . catch_warnings ( record = True ) : mem = int ( round ( max ( memory_usage ( ( val , ( ) , { } ) ) ) ) ) else : val ( ) mem = ( - 1 ) if ( mem >= peak_mem ) : ( peak_mem , peak_name ) = ( mem , name ) mem = ( ( ', mem: %s MB' % mem ) if ( mem >= 0 ) else '' ) elapsed = int ( round ( ( time . time ( ) - t1 ) ) ) if ( elapsed >= max_elapsed ) : ( max_elapsed , elapsed_name ) = ( elapsed , name ) sys . stdout . write ( ( 'time: %s sec%s\n' % ( elapsed , mem ) ) ) sys . stdout . flush ( ) except Exception as err : if ( 'skiptest' in err . __class__ . __name__ . lower ( ) ) : sys . stdout . write ( ( 'SKIP (%s)\n' % str ( err ) ) ) sys . stdout . flush ( ) else : raise elapsed = int ( round ( ( time . time ( ) - t0 ) ) ) sys . stdout . write ( ( 'Total: %s tests\n\xe2\x80\xa2 %s sec (%s sec for %s)\n\xe2\x80\xa2 Peak memory %s MB (%s)\n' % ( count , elapsed , max_elapsed , elapsed_name , peak_mem , peak_name ) ) )
run tests in a given file if it is run as a script .