py.test - custom assert in pytest should overrule standard assert -
i wrote custom assert function compare items in 2 lists such order not important, using pytest_assertrepr_compare. works fine , reports failure when content of lists differ.
however, if custom assert passes, fails on default '==' assert because item 0 of 1 list unequal item 0 of other list.
is there way prevent default assert kick in?
assert ['a', 'b', 'c'] == ['b', 'a', 'c'] # custom assert passes # default assert fails the custom assert function is:
def pytest_assertrepr_compare(config, op, left, right): equal = true if op == '==' , isinstance(left, list) , isinstance(right, list): if len(left) != len(right): equal = false else: l in left: if not l in right: equal = false if equal: r in right: if not r in left: equal = false if not equal: return ['comparing lists:', ' vals: %s != %s' % (left, right)]
i found easiest way combinate py.test & pyhamcrest. in example easy use contains_inanyorder matcher:
from hamcrest import assert_that, contains_inanyorder def test_first(): assert_that(['a', 'b', 'c'], contains_inanyorder('b', 'a', 'c'))
Comments
Post a Comment