Best Python code snippet using avocado_python
tracing.py
Source:tracing.py
1import os2import inspect3def trace_function(function, log_calls, log_results, label=''):4 def wrapper(*args, **kwargs):5 kwarg_strs = ['{}={}'.format(k, v) for (k, v) in kwargs]6 arg_str = ', '.join([str(a) for a in args] + kwarg_strs)7 call_str = '{}({})'.format(function.func_name, arg_str)8 # Perform the call itself, logging before, after, and anything thrown.9 try:10 if log_calls:11 print('{}: Calling {}'.format(label, call_str))12 res = function(*args, **kwargs)13 if log_results:14 print('{}: {} -> {}'.format(label, call_str, res))15 return res16 except Exception as ex:17 if log_results:18 print('{}: {} raised {}'.format(label, call_str, type(ex)))19 raise ex20 return wrapper21def trace_object(obj, log_calls, log_results, label=''):22 for name, member in inspect.getmembers(obj):23 if inspect.ismethod(member):24 # Skip meta-functions, decorate everything else25 if not member.func_name.startswith('__'):26 setattr(obj, name, trace_function(member, log_calls,27 log_results, label))...
log_calls_decorator.py
Source:log_calls_decorator.py
1import time2def log_calls(func):3 def wrapper(*args, **kwargs):4 now = time.time()5 print(6 "Calling {0} with {1} and {2}".format(7 func.__name__, args, kwargs8 )9 )10 return_value = func(*args, **kwargs)11 print(12 "Executed {0} in {1}ms".format(13 func.__name__, time.time() - now14 )15 )16 return return_value17 return wrapper18def test1(a, b, c):19 print("\ttest1 called")20def test2(a, b):21 print("\ttest2 called")22def test3(a, b):23 print("\ttest3 called")24 time.sleep(1)25test1 = log_calls(test1)26test2 = log_calls(test2)27test3 = log_calls(test3)28test1(1, 2, 3)29test2(4, b=5)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!