[英]Python and pytest - check if a test passes or fails and execute code
I'm doing test automation with python and we have a test management tool.我正在使用 python 进行测试自动化,我们有一个测试管理工具。 What we try to do is update the test cases in this management tool so that if a test fails the guys here can see it very quickly.我们尝试做的是更新这个管理工具中的测试用例,这样如果测试失败,这里的人可以很快看到它。
But the thing is that I can't figure out when I test has passed or failed.但问题是我无法弄清楚我的测试何时通过或失败。 So my tests looks like this:所以我的测试看起来像这样:
@decorator(testId = 5, comment = "Test fails or not")
def test(self):
# assert
assert True == False
No i'm trying to solve it with a decorator but it didn't work out.不,我正试图用装饰器来解决它,但没有成功。 So this test will fail and now I need to push that to a test management system online.所以这个测试将失败,现在我需要将它推送到在线测试管理系统。 Anybody a clue on how to do this?有人知道如何做到这一点吗? My decorator code looks like this:我的装饰器代码如下所示:
def decorator(*args, **kwargs):
print("Inside decorator")
def inner(func):
print("testId: ", kwargs["testId"])
if "comment" in kwargs.keys():
print("comment: ", kwargs["comment"])
try:
pass
except AssertionError as assertionError:
print("Assertion fails: " + str(assertionError))
raise
except Exception as ex:
print("Something else failed")
print("exception: " + str(ex))
raise
finally:
print("ending")
return func
return inner
After searching for a while, I made a better decorator.经过一段时间的搜索,我做了一个更好的装饰器。 It looks like this:它看起来像这样:
import functools
def exception(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
try:
return function(*args, **kwargs)
except:
print("error in: " + function.__name__)
raise
return wrapper
@exception
def zero_divide():
1 / 1
a = AnObjectTHatDOESNTEXIST()
print("start")
zero_divide()
print("end")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.