python断言函数的建议
我在多个脚本中多次使用断言,我想知道是否有人对实现此目的的更好方法有任何建议,而不是我在下面创建的函数。
def assert_validation(expected, actual, type='', message=''):
if type == '==':
assert expected == actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '!=':
assert expected != actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '<=':
assert expected <= actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '>=':
assert expected >= actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
def assert_str_validation(expected, actual, type='', message=''):
if type == '==':
assert str(expected) == str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '!=':
assert str(expected) != str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '<=':
assert str(expected) <= str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '>=':
assert str(expected) >= str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
I'm using assert multiple times throughout multiple scripts, I was wondering if anyone has any suggestions on a better way to achieve this instead of the functions I have created below.
def assert_validation(expected, actual, type='', message=''):
if type == '==':
assert expected == actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '!=':
assert expected != actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '<=':
assert expected <= actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '>=':
assert expected >= actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
def assert_str_validation(expected, actual, type='', message=''):
if type == '==':
assert str(expected) == str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '!=':
assert str(expected) != str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '<=':
assert str(expected) <= str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
elif type == '>=':
assert str(expected) >= str(actual), 'Expected: %s, Actual: %s, %s' %(expected, actual, message)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这肯定更短......你真的不能只使用
assert Expected ==actual
或脚本本身中的其他内容吗?Well this is certainly shorter... can you really not just use
assert expected == actual
or whatever in the scripts themselves?