2
我正在使用pytest來自動化項目測試。我只想在測試用例失敗時採取一些獨特的操作,比如「save_snapshot()」。如何檢測pytest測試用例何時得到AssertionError?
我們在pytest中有類似的東西嗎?
我試圖用teardown_method()來實現這個,但是當測試用例失敗時,這個方法沒有得到執行。
- 請不要使用燈具。
我正在使用pytest來自動化項目測試。我只想在測試用例失敗時採取一些獨特的操作,比如「save_snapshot()」。如何檢測pytest測試用例何時得到AssertionError?
我們在pytest中有類似的東西嗎?
我試圖用teardown_method()來實現這個,但是當測試用例失敗時,這個方法沒有得到執行。
我發現這個問題的解決方案通過使用python裝飾在課堂上每個測試:
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
工作對我來說:d
儘量使用像[pytest_runtest_teardown] pytest插件API(HTTP ://doc.pytest.org/en/latest/writing_plugins.html#_pytest.hookspec.pytest_runtest_teardown)和家人。 –
@AndrewSvetlov你能評論我的想法嗎?我會爲你的意見感到高興 – nivhanin