Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yashaka/0cb814a5de49f4dc91a3a8528bf0d6e7 to your computer and use it in GitHub Desktop.
Save yashaka/0cb814a5de49f4dc91a3a8528bf0d6e7 to your computer and use it in GitHub Desktop.
The code from lesson «Python Decorators to Log Test Steps»
import types
from functools import wraps
def humanify(name: str):
import re
return ' '.join(re.split('_+', name))
def step(fn):
@wraps(fn)
def fn_with_logging(*args, **kwargs):
is_method = (
args
and isinstance(args[0], object)
and isinstance(getattr(args[0], fn.__name__), types.MethodType)
)
args_to_log = args[1:] if is_method else args
args_and_kwargs_to_log_as_strings = [
*map(str, args_to_log),
*[f'{key}={value}' for key, value in kwargs.items()]
]
args_and_kwargs_string = (
(': ' + ', '.join(map(str, args_and_kwargs_to_log_as_strings)))
if args_and_kwargs_to_log_as_strings
else ''
)
print(
(f'[{args[0].__class__.__name__}] ' if is_method else '')
+ humanify(fn.__name__)
+ args_and_kwargs_string
)
return fn(*args, **kwargs)
return fn_with_logging
'''
def given_sign_up_form_opened():
log_step(given_sign_up_form_opened.__name__)
...
given_sign_up_form_opened = step(given_sign_up_form_opened)
'''
@step
def given_sign_up_form_opened():
...
class SignUpForm:
@step
def fill_name(self, first_name, surname):
pass
return self
@step
def fill_email(self, value):
pass
return self
@step
def fill_password(self, value):
pass
return self
@step
def submit(self):
pass
return self
class DashBoard:
...
@step
def go_to_user_profile(self):
pass
sign_up_form = SignUpForm()
dashboard = DashBoard()
given_sign_up_form_opened()
(sign_up_form
.fill_name('yasha', surname='Kramarenko')
.fill_email('yashaka@gmail.com')
.fill_password('qwerty')
.submit()
)
dashboard.go_to_user_profile()
'''
# will log to console the following:
given sign up form opened
[SignUpForm] fill name: yasha, surname=Kramarenko
[SignUpForm] fill email: yashaka@gmail.com
[SignUpForm] fill password: qwerty
[SignUpForm] submit
[DashBoard] go to user profile
Process finished with exit code 0
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment