python断言函数的建议

发布于 2024-07-29 05:11:58 字数 1217 浏览 2 评论 0原文

我在多个脚本中多次使用断言,我想知道是否有人对实现此目的的更好方法有任何建议,而不是我在下面创建的函数。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2024-08-05 05:11:58

嗯,这肯定更短......你真的不能只使用 assert Expected ==actual 或脚本本身中的其他内容吗?

def assert_validation(expected, actual, type='', message='', trans=(lambda x: x)):
    m = { '==': (lambda e, a: e == a),
          '!=': (lambda e, a: e != a),
          '<=': (lambda e, a: e <= a),
          '>=': (lambda e, a: e >= a), }
    assert m[type](trans(expected), trans(actual)), 'Expected: %s, Actual: %s, %s' % (expected, actual, message)

def assert_str_validation(expected, actual, type='', message=''):
    assert_validation(expected, actual, type, message, trans=str)

Well this is certainly shorter... can you really not just use assert expected == actual or whatever in the scripts themselves?

def assert_validation(expected, actual, type='', message='', trans=(lambda x: x)):
    m = { '==': (lambda e, a: e == a),
          '!=': (lambda e, a: e != a),
          '<=': (lambda e, a: e <= a),
          '>=': (lambda e, a: e >= a), }
    assert m[type](trans(expected), trans(actual)), 'Expected: %s, Actual: %s, %s' % (expected, actual, message)

def assert_str_validation(expected, actual, type='', message=''):
    assert_validation(expected, actual, type, message, trans=str)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文