Reputation: 1918
I am using pytest to automate project test. I want to take some unique actions like "save_snapshot()" only when a test case fails.
Do we have something like that in pytest?
I have tried to achieve this using the teardown_method() But this method is not getting executed when a test case fails.
Upvotes: 2
Views: 692
Reputation: 1918
I found a solution for this issue by using python decorator for each test in class:
def is_failed_decorator(func):
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except AssertionError:
cls_obj = args[0]
cur_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
func_name = func.__name__
# Save_snapshot().
raise
return wrapper
# Tests class
@is_failed_decorator
def test_fail(self):
assert False
worked for me :D
Upvotes: 4